create an empty BitmapSource in C#
Asked Answered
A

6

9

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.

Angstrom answered 26/8, 2010 at 9:40 Comment(0)
B
14

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);
Buran answered 26/8, 2010 at 9:46 Comment(0)
A
16

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.

Angstrom answered 26/8, 2010 at 11:23 Comment(2)
You can, by using a different format (indexed formats are more peculiar, but I don't know the exact reason either). For example: BitmapSource.Create(1, 1, 96, 96, PixelFormats.Bgra32, null, new byte[] { 0, 0, 0, 0 }, 4) (in this example, the stride is four because there are four bytes per pixel in Bgra32, and the four bytes in the array describe the one pixel). edit: Actually, I think your example should work too, if you shorten the byte array to one element for one pixel.Guaiacol
using your parameters (1, 1, 96, 96, PixelFormats.Bgra32, null, new byte[] { 0, 0, 0, 0 }, 4) will prevent the whole WPF UI from rendering.Angstrom
B
14

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);
Buran answered 26/8, 2010 at 9:46 Comment(0)
P
6

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));
Periodic answered 2/2, 2016 at 2:9 Comment(0)
P
3

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);
    }
Parisian answered 13/5, 2018 at 13:53 Comment(1)
This will create an image that is visible (i.e. a black pixel).Angstrom
M
3

Another way is to create an instance of a BitmapImage class which is derived from BitmapSource:

BitmapSource emptySource = new BitmapImage();

Melanie answered 5/3, 2019 at 10:32 Comment(0)
I
1

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;
    }
Immovable answered 17/4, 2015 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.