Saving PNG with alpha channel - wpf

I'm trying to save a WriteableBitmap to png but always end up with a 24 bit image (no alpha channel).
WriteableBitmap image = new WriteableBitmap(100, 100, 600, 600, PixelFormats.Bgra32, null);
int stride = image.PixelWidth * image.Format.BitsPerPixel / 8;
image.WritePixels(new System.Windows.Int32Rect(0, 0, image.PixelWidth, image.PixelHeight), emptyArray, stride, 0);
FileStream filestream = new FileStream(imageSrc, FileMode.Create);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((image)));
encoder.Save(filestream);
emptyArray is an array with all pixels being (255, 0, 0, 0) so I can test if the saving has worked.
Any ideas?

I managed to find the problem: the file was altered some place else and overwrote the intial image. So the initial saving of the image worked fine. Sorry for the hassle!

Related

How do I set a WPF Image's Source to a bytearray in C# code behind?

I'm building a small app using C#/WPF.
The application receives (from an unmanaged C++ library) a byte array (byte[]) from a bitmap source
In my WPF window, I have an (System.windows.Controls.Image) image which I will use to display the bitmap.
In the code behind (C#) I need to able to take that byte array, create BitmapSource /ImageSource and assign the source for my image control.
// byte array source from unmanaged librariy
byte[] imageData;
// Image Control Definition
System.Windows.Controls.Image image = new Image() {width = 100, height = 100 };
// Assign the Image Source
image.Source = ConvertByteArrayToImageSource(imageData);
private BitmapSource ConvertByteArrayToImagesource(byte[] imageData)
{
??????????
}
I've been working on this for a bit here and haven't been able to figure this out. I've tried several solutions that I've found by goolging around. To date, I haven't been able to figure this out.
I've tried:
1) Creating a BitmapSource
var stride = ((width * PixelFormats.Bgr24 +31) ?32) *4);
var imageSrc = BitmapSource.Create(width, height, 96d, 96d, PixelFormats.Bgr24, null, imageData, stride);
That through a runtime exception saying buffer was too small
Buffer size is not sufficient
2) I tried using a memory stream:
BitmapImage bitmapImage = new BitmapImage();
using (var mem = new MemoryStream(imageData))
{
bitmapImage.BeginInit();
bitmapImage.CrateOptions = BitmapCreateOptions.PreservePixelFormat;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = mem;
bitmapImage.EndInit();
return bitmapImage;
}
This code through an exception on the EndInit() call.
"No imaging component suitableto complete this operation was found."
SOS! I've spent a couple of days on this one and am clearly stuck.
Any help/ideas/direction would be greatly appreciated.
Thanks,
JohnB
Your stride calculation is wrong. It is the number of full bytes per scan line, and should therefore be calculated like this:
var format = PixelFormats.Bgr24;
var stride = (width * format.BitsPerPixel + 7) / 8;
var imageSrc = BitmapSource.Create(
width, height, 96d, 96d, format, null, imageData, stride);
Of course you also have to make sure that you use the correct image size, i.e. that the width and height values actually correspond with the data in imageBuffer.

BitmapImage from BitmapSource always Bgr32

I'm loading a BitmapImage from a BitmapSource and i always find the BitmapImage's format is Bgr32 instead of Bgra32 which the BitmapSource is. There is a similar thread on this here:
BitmapImage from file PixelFormat is always bgr32
but using BitmapCreateOptions.PreservePixelFormat didn't work for me as was suggested in the thread. here's what i'm doing:
// I have verified that b is a valid BitmapSource and its format is Bgra32
// the following code produces a file (testbmp2.bmp) with an alpha channel as expected
// placing a breakpoint here and querying b.Format in the Immediate window also produces
// a format of Bgra32
BmpBitmapEncoder test = new BmpBitmapEncoder();
FileStream stest = new FileStream(#"c:\temp\testbmp2.bmp", FileMode.Create);
test.Frames.Add(BitmapFrame.Create(b));
test.Save(stest);
stest.Close();
// however, this following snippet results in bmp.Format being Bgr32
BitmapImage bmp = new BitmapImage();
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
MemoryStream stream = new MemoryStream();
encoder.Frames.Add(BitmapFrame.Create(b));
encoder.Save(stream);
bmp.BeginInit();
bmp.StreamSource = new MemoryStream(stream.ToArray());
bmp.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
bmp.CacheOption = BitmapCacheOption.None;
bmp.EndInit();
stream.Close();
Is there a way to create a BitmapImage from the BitmapSource and preserver the alpha channel?
Update:
I used the same code posted in the the other thread to load testbmp2.bmp from file and after loading, the Format property is Bgr32.
BitmapImage b1 = new BitmapImage();
b1.BeginInit();
b1.UriSource = new Uri(#"c:\temp\testbmp2.bmp");
b1.CreateOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreImageCache;
b1.CacheOption = BitmapCacheOption.OnLoad;
b1.EndInit();
so maybe i'm not saving the FileStream/MemoryStream correctly? That doesn't seem right since after saving the FileStream, I can open testbmp2.bmp in Photoshop and see the alpha channel.
Update 2:
I think I need to re-word the issue I'm having. I'm trying to display separate channels of a texture. I am displaying the bitmap via an Image control. I have a simple HLSL pre compiled shader assigned to the Image's Effect property that will mask out the channels based user input. Not being able to get the BitmapSource into a BitmapImage while retaining the alpha channel was only part of the problem. I realize now that since my original BitmapSource's Format was Bgra32 I could assign that directly to the Image's Source property. The problem appears to be that an Image object will only display texels with pre-multiplied alpha...? Here's my shader code:
sampler2D inputImage : register(s0);
float4 channelMasks : register(c0);
float4 main (float2 uv : TEXCOORD) : COLOR0
{
float4 outCol = tex2D(inputImage, uv);
if (!any(channelMasks.rgb - float3(1, 0, 0)))
{
outCol.rgb = float3(outCol.r, outCol.r, outCol.r);
}
else if (!any(channelMasks.rgb - float3(0, 1, 0)))
{
outCol.rgb = float3(outCol.g, outCol.g, outCol.g);
}
else if (!any(channelMasks.rgb - float3(0, 0, 1)))
{
outCol.rgb = float3(outCol.b, outCol.b, outCol.b);
}
else
{
outCol *= channelMasks;
}
if (channelMasks.a == 1.0)
{
outCol.r = outCol.a; // * 0.5 + 0.5;
outCol.g = outCol.a;
outCol.b = outCol.a;
}
outCol.a = 1;
return outCol;
}
I've tested it pretty extensively and I'm pretty sure that its setting the outCol value properly. When i pass in a channel mask value of (1.0, 1.0, 1.0, 0.0) the image displayed is the RGB channels with the areas of the alpha that are black drawing as black - as you would expect if WPF was pre-multiplying the alpha on to the RGB channels. Does anyone know of any way to dispaly a BitmapSource in an Image without pre multiplied alpha? Or more to the point, is there a way to have the effect receive the texture before the alpha pre-multiply happens? I'm not sure what order things are done in, but apparently the pre-multiply happens before the texture is written to the s0 register and the shader gets to work on it. I could try to do this with WriteableBitmap and copying the alpha out to another BitmapSource but that would all be using software rendering i think...?
I ran into a similar problem lately. The BmpBitmapEncoder does not care about the alpha channel and therefore does not preserve it. An easy fix is to use a PngBitmapEncoder instead.

Workaround for PictureBox Zoom antialiasing bug?

A PictureBox which has SizeMode.Zoom and enlarges the source image adds white at the bottom and right edges. Anyone have a simple proven workaround? I beleive the bug is in GDI+ so the obvious custom repaint will not fix it. Currently I'm using WPF which I want to avoid.
I'll even settle for a kluge that replaces the bad white with black :)
Example: from a source having single central white pixel, instead of this:
it gives this:
The code is:
private void PictureBoxZoomBug()
{
BitmapSource bitmapSource =
BitmapSource.Create(3, 3, 96, 96, System.Windows.Media.PixelFormats.Gray8, null,
new byte[] { 0, 0, 0, 0, 0xFF, 0, 0, 0, 0 }, 3);
TestDiscamImage.Source = bitmapSource; // OK
Bitmap bitmap;
using (MemoryStream stream = new MemoryStream())
{
PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapSource));
enc.Save(stream);
bitmap = new System.Drawing.Bitmap(stream);
}
TestDiscamCapture_pictureBox.Image = bitmap; // Extra white at bottom and right
}
Thanks

Silverlight 4.0: How to determine the file size of an object in MemoryStream

byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
How will I determine its file size of an image?
You should be able to determine the size of the stream pretty easy.
MemoryStream ms = new MemoryStream();
int length = ms.Length;
length is now the length of the stream in bytes. This bytenumber should also be the size of any file you would store that contained only this stream.
Edit:
If you mean in pixels you could use something like:
Image img = Image.FromStream(ms);
int width = img.Width;
int height = img.Height;

WinForms .NET 2.0: How to paint the proper sized icon?

i have an ico file that contains a 48x48 and a 256x256 Vista PNG version (as well as the 32x32 and 16x16 versions). i want to draw the icon using the appropriate internal size version.
i've tried:
Icon ico = Properties.Resources.TestIcon;
e.Graphics.DrawIcon(ico, new Rectangle(0, 0, 48, 48));
e.Graphics.DrawIcon(ico, new Rectangle(48, 0, 256, 256));
But they draw the 32x32 version blown up to 48x48 and 256x256 respectively.
i've tried:
Icon ico = Properties.Resources.TestIcon;
e.Graphics.DrawIconUnstretched(ico, new Rectangle(0, 0, 48, 48));
e.Graphics.DrawIconUnstretched(ico, new Rectangle(48 0, 256, 256));
But those draw the 32x32 version unstretched.
i've tried:
Icon ico = Properties.Resources.TestIcon;
e.Graphics.DrawImage(ico.ToBitmap(), new Rectangle(0, 0, 48, 48));
e.Graphics.DrawImage(ico.ToBitmap(), new Rectangle(48, 0, 256, 256));
But those draw a stretched version of the 32x32 icon.
How do i make the icon draw itself using the appropriate size?
Additionally, i want to draw using the 16x16 version. i've tried:
Icon ico = Properties.Resources.TestIcon;
e.Graphics.DrawIcon(ico, new Rectangle(0, 0, 16, 16));
e.Graphics.DrawIconUnstretched(ico, new Rectangle(24, 0, 16, 16));
e.Graphics.DrawImage(ico.ToBitmap(), new Rectangle(48, 0, 16, 16));
But all those use the 32x32 version scaled down, except for the Unstretched call, which crops it to 16x16.
How do i make the icon draw itself using the appropriate size?
Following schnaader's suggestion of constructing a copy of the icon with the size you need doesn't work for 256x256 size. i.e. the following does not work (it uses a scaled version of the 48x48 icon):
e.Graphics.DrawIcon(
new Icon(ico, new Size(256, 256)),
new Rectangle(0, 0, 256, 256));
While the following two do work:
e.Graphics.DrawIcon(
new Icon(ico, new Size(16, 16)),
new Rectangle(0, 0, 16, 16));
e.Graphics.DrawIcon(
new Icon(ico, new Size(48, 48)),
new Rectangle(0, 0, 48, 48));
Today, I made a very nice function for extracting the 256x256 Bitmaps from Vista icons.
I use it to display the large icon ( 256x256 ) as a Bitmap in "About" box. For example, this code gets Vista icon as PNG image, and displays it in a 256x256 PictureBox:
picboxAppLogo.Image = ExtractVistaIcon(Icon.ExtractAssociatedIcon(myIcon));
This function takes Icon object as a parameter. So, you can use it with any icons - from resources, from files, from streams, and so on. (Read below about extracting EXE icon).
It runs on any OS, because it does not use any Win32 API, it is 100% managed code :-)
// Based on: http://www.codeproject.com/KB/cs/IconExtractor.aspx
// And a hint from: http://www.codeproject.com/KB/cs/IconLib.aspx
Bitmap ExtractVistaIcon(Icon icoIcon)
{
Bitmap bmpPngExtracted = null;
try
{
byte[] srcBuf = null;
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{ icoIcon.Save(stream); srcBuf = stream.ToArray(); }
const int SizeICONDIR = 6;
const int SizeICONDIRENTRY = 16;
int iCount = BitConverter.ToInt16(srcBuf, 4);
for (int iIndex=0; iIndex<iCount; iIndex++)
{
int iWidth = srcBuf[SizeICONDIR + SizeICONDIRENTRY * iIndex];
int iHeight = srcBuf[SizeICONDIR + SizeICONDIRENTRY * iIndex + 1];
int iBitCount = BitConverter.ToInt16(srcBuf, SizeICONDIR + SizeICONDIRENTRY * iIndex + 6);
if (iWidth == 0 && iHeight == 0 && iBitCount == 32)
{
int iImageSize = BitConverter.ToInt32(srcBuf, SizeICONDIR + SizeICONDIRENTRY * iIndex + 8);
int iImageOffset = BitConverter.ToInt32(srcBuf, SizeICONDIR + SizeICONDIRENTRY * iIndex + 12);
System.IO.MemoryStream destStream = new System.IO.MemoryStream();
System.IO.BinaryWriter writer = new System.IO.BinaryWriter(destStream);
writer.Write(srcBuf, iImageOffset, iImageSize);
destStream.Seek(0, System.IO.SeekOrigin.Begin);
bmpPngExtracted = new Bitmap(destStream); // This is PNG! :)
break;
}
}
}
catch { return null; }
return bmpPngExtracted;
}
IMPORTANT! If you want to load this icon directly from EXE file, then you CAN'T use Icon.ExtractAssociatedIcon(Application.ExecutablePath) as a parameter, because .NET function ExtractAssociatedIcon() is so stupid, it extracts ONLY 32x32 icon!
Instead, you better use the whole IconExtractor class, created by Tsuda Kageyu (http://www.codeproject.com/KB/cs/IconExtractor.aspx). You can slightly simplify this class, to make it smaller. Use IconExtractor this way:
// Getting FILL icon set from EXE, and extracting 256x256 version for logo...
using (TKageyu.Utils.IconExtractor IconEx = new TKageyu.Utils.IconExtractor(Application.ExecutablePath))
{
Icon icoAppIcon = IconEx.GetIcon(0); // Because standard System.Drawing.Icon.ExtractAssociatedIcon() returns ONLY 32x32.
picboxAppLogo.Image = ExtractVistaIcon(icoAppIcon);
}
Note: I'm still using my ExtractVistaIcon() function here, because I don't like how IconExtractor handles this job - first, it extracts all icon formats by using IconExtractor.SplitIcon(icoAppIcon), and then you have to know the exact 256x256 icon index to get the desired vista-icon. So, using my ExtractVistaIcon() here is much faster and simplier way :)

Resources