windows bitmap with alpha

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
Fenriss
Posts: 2
Joined: 2017-06-24T03:54:26-07:00
Authentication code: 1151

windows bitmap with alpha

Post by Fenriss »

I had a hard time creating a 32bit rgba bitmap with Magick.NET-Q16-AnyCPU / 7.0.0.0 which i got via visual studio 2017 nuget manager.

Trying to create an image with

Code: Select all

var bitmap = new MagickImage(MagickColor.FromRgba(0, 0, 0, 255), size, size);
somehow disabled the alpha channel, but

Code: Select all

var bitmap = new MagickImage(MagickColor.FromRgba(0, 0, 0, 0), size, size);
didn't?
As i couldn't find a function to fill the whole image, like drawing a filled rectangle without border which is the size of the image, i ended up creating a black image with alpha 255 like this:

Code: Select all

var bitmap = new MagickImage(MagickColor.FromRgb(0, 0, 0), size, size);
bitmap.Transparent(MagickColor.FromRgb(0, 0, 0));
It feels a little too unintuitive. Is there a better approach? Why behaves MagickImage(MagickColor.FromRgba(0, 0, 0, 255) different from MagickImage(MagickColor.FromRgba(0, 0, 0, 0) ? This is really puzzling me.


Anyway, when trying to save the image as 32bit windows bmp, alpha got stripped away and the file saved was 24bit:

Code: Select all

using (var os = new System.IO.FileStream("asdf.bmp", System.IO.FileMode.Create))
{
  bitmap.Write(os, MagickFormat.Bmp3);
}
Now, i read here and then there about Bmp4 and some support for Bmp3 as well but since I'm new to Magick.NET, i honestly don't have a clue what to do. Is that nuget Version too old or did I just load the wrong one?

Anyway, luckily, there's a ToBitmap member in MagickImage which seems to preserve all 32bit, so at the moment i ended up using this:

Code: Select all

var bmp = bitmap.ToBitmap(System.Drawing.Imaging.ImageFormat.Bmp);
bmp.Save("asdf.bmp");
However, i would feel better using only ImageMagick types to save the bitmap.
Can this be done with Magick.NET? Is the mentioned Bmp4 type already released in a newer Magick.NET package? Is there a flag i can set which mimics the "-define bmp3:alpha=true" mentioned in that link?
Last edited by Fenriss on 2017-06-27T13:37:11-07:00, edited 1 time in total.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: windows bitmap with alpha

Post by dlemstra »

When you create an image with

Code: Select all

MagickColor.FromRgba(0, 0, 0, 255)
the alpha channel won't be turned on because the alpha value is opaque. But when you use

Code: Select all

MagickColor.FromRgba(0, 0, 0, 0)
the alpha channel will be enabled. When the image is created it looks if the alpha channel is enabled and it won't be enabled in the first case but it will be enabled in the second case. That is why you are getting a different result.

What you could do is enable the alpha channel. The default bmp format is BMP4 so you don't need to worry about that bmp3: option. The following code will force the creation of a 32-bit all black image:

Code: Select all

using (MagickImage image = new MagickImage(MagickColor.FromRgb(0, 0, 0), 100, 100))
{
    image.Alpha(AlphaOption.Set);
    image.Write(@"i:\test.bmp");
}
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Fenriss
Posts: 2
Joined: 2017-06-24T03:54:26-07:00
Authentication code: 1151

Re: windows bitmap with alpha

Post by Fenriss »

Stupid me, didn't try the Write method with no format argument. Works like a charm!

However, although I forgot to mention this requirement explicitely,

Code: Select all

using (MagickImage bitmap = new MagickImage(MagickColor.FromRgb(0, 0, 0), outputImageWidth, outputImageWidth)) {
    bitmap.Transparent(MagickColor.FromRgb(0, 0, 0));
initializes alpha to zero where

Code: Select all

using (MagickImage image = new MagickImage(MagickColor.FromRgb(0, 0, 0), 100, 100)) {
    image.Alpha(AlphaOption.Set);
initializes it to max.

Is there another method which allows to set the initial alpha value?
There would be flood fill of course, but it seems overkill :D
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: windows bitmap with alpha

Post by dlemstra »

If you want the pixels to be transparent you could do this:

Code: Select all

image.Alpha(AlphaOption.Transparent);
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply