create an empty BitmapSource in C#

bitbonk picture bitbonk · Aug 26, 2010 · Viewed 18.5k times · Source

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.

Answer

bitbonk picture bitbonk · Aug 26, 2010

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.