create an empty BitmapSource in C#

bitmap to bitmapsource c#
c# create bitmap
create a blank image c#
bitmapsource.create stride
c# generate blank image
wpf create bitmap
wpf image to bitmapsource
c# bitmapsource to bitmapimage

What is the fastest (few lines of code and low resource usage) way to create an empty (0x0 px or 1x1 px and fully transparent) BitmapSource instance in c# that is used when nothing should be rendered.


Use the Create method.

Example stolen from MSDN: :)

int width = 128;
int height = width;
int stride = width/8;
byte[] pixels = new byte[height*stride];

// Try creating a new image with a custom palette.
List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>();
colors.Add(System.Windows.Media.Colors.Red);
colors.Add(System.Windows.Media.Colors.Blue);
colors.Add(System.Windows.Media.Colors.Green);
BitmapPalette myPalette = new BitmapPalette(colors);

// Creates a new empty image with the pre-defined palette
BitmapSource image = BitmapSource.Create(
                                         width, height,
                                         96, 96,
                                         PixelFormats.Indexed1,
                                         myPalette, 
                                         pixels, 
                                         stride);

How to: Create a new BitmapSource, This example demonstrates how to use the Create method of BitmapSource to create a new BitmapSource object. Example. C# Copy. // Define  Create(Int32, Int32, Double, Double, PixelFormat, BitmapPalette, Array, Int32) Creates a new BitmapSource from an array of pixels. Create(Int32, Int32, Double, Double, PixelFormat, BitmapPalette, IntPtr, Int32, Int32) Creates a new BitmapSource from an array of pixels that are stored in unmanaged memory.


Thanks to Arcutus hint I have this now (wich works fine):

var i = BitmapImage.Create(
    2,
    2,
    96,
    96,
    PixelFormats.Indexed1,
    new BitmapPalette(new List<Color> { Colors.Transparent }),
    new byte[] { 0, 0, 0, 0 },
    1);

If I make this image smaller I get an ArgumentException. I have no clue why I can't create a smaller image that 2x2px.

BitmapSource.Create Method (System.Windows.Media.Imaging , Creates a new BitmapSource from an array of pixels that are stored in C# Copy​. public static System.Windows.Media.Imaging.BitmapSource Create (int Creates a new empty image with the pre-defined palette BitmapSource image  ' Define parameters used to create the BitmapSource. Dim pf As PixelFormat = PixelFormats.Bgr32 Dim width As Integer = 200 Dim height As Integer = 200 Dim rawStride As Integer = CType ( (width * pf.BitsPerPixel + 7) / 8, Integer) Dim rawImage (rawStride * height) As Byte ' Initialize the image with data.


The way to create such an image without allocating a big managed byte array is to use TransformedBitmap.

var bmptmp = BitmapSource.Create(1,1,96,96,PixelFormats.Bgr24,null,new byte[3]{0,0,0},3);

var imgcreated = new TransformedBitmap(bmptmp, new ScaleTransform(width, height));

BitmapFrame.Create Method (System.Windows.Media.Imaging , Creates a new BitmapFrame from a given BitmapSource with the specified C# Copy. public static System.Windows.Media.Imaging.BitmapFrame Create Creates a new empty image with the pre-defined palette BitmapSource image  Create(BitmapSource) Creates a new BitmapFrame from a given BitmapSource. Create(Uri, RequestCachePolicy) Creates a BitmapFrame from a given Uri with the specified RequestCachePolicy. Create(BitmapSource, BitmapSource) Creates a new BitmapFrame from a given BitmapSource with the specified thumbnail. Create(Stream, BitmapCreateOptions, BitmapCacheOption)


The most minimal BitmapSource can be generated like this:

    public static BitmapSource CreateEmptyBitmap()
    {
        return BitmapSource.Create(1, 1, 1, 1, PixelFormats.BlackWhite, null, new byte[] {0}, 1);
    }

BitmapFrame Class (System.Windows.Media.Imaging), C# Copy. public abstract class BitmapFrame : System.Windows.Media.Imaging. Creates a new empty image with the pre-defined palette BitmapSource image​  How to: Create a Bitmap at Run Time. 03/30/2017; 2 minutes to read +6; In this article. This example creates and draws in a Bitmap object and displays it in an existing Windows Forms PictureBox control.


Just take a look at this. It works for any Pixelformat

  public static BitmapSource CreateEmtpyBitmapSource(int width, int height, PixelFormat pixelFormat)
    {
        PixelFormat pf = pixelFormat;
        int rawStride = (width * pf.BitsPerPixel + 7) / 8;
        var rawImage = new byte[rawStride * height];
        var bitmap = BitmapSource.Create(width, height, 96, 96, pf, null, rawImage, rawStride);
        return bitmap;
    }

How to: Use a BitmapImage, Width = 200; // Create source BitmapImage myBitmapImage = new BitmapImage​(); // BitmapImage.UriSource must be in a BeginInit/EndInit  ' Define parameters used to create the BitmapSource. Dim pf As PixelFormat = PixelFormats.Bgr32 Dim width As Integer = 200 Dim height As Integer = 200 Dim rawStride As Integer = CType((width * pf.BitsPerPixel + 7) / 8, Integer) Dim rawImage(rawStride * height) As Byte ' Initialize the image with data.


Convert byte[] to BitmapImage in WPF application using c# , BitsPerPixel + 31) / 32) * 4; var image = BitmapSource.Create(width, height, 96d, 96d, format, null, buffer, stride); return (CachedBitmap)image;  hey guys, I have a Form with a Picture Box, whereby upon loading of the form the Image in the Picture Box is a White Bitmap Image, I have been using a Brute force Method so far being Bitmap B = new


Create Blank White Bitmap Image, Create Blank White Bitmap Image. Feb 19 2009 6:24 AM. hey guys, I have a Form with a Picture Box, whereby upon loading of the form the Image in the Picture  Creating an empty bitmap and drawing though canvas in Android. I'd like to create an empty bitmap and set a canvas to that bitmap and then draw any shape on the


BitmapSource Class, C#. VB. C++. F#. Copy. public sealed class BitmapSource : BitmapSource, Creates an empty BitmapSource. Public method, BitmapSource(Image, Int32, Int32, HighBitScaleMode). Creates a BitmapSource from the given source image. @StaWho In fact I need BitmapImage because I have to create ImageBrush ib = new ImageBrush(bitmapImage); – Developer May 17 '12 at 14:14 1 You can create ImageBrush from BitmapSource .