Search code examples
c#wpfbitmapsource

create an empty BitmapSource in C#


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.


Solution

  • 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);