Button with transparent png image in .Net WPF - c#

I have a WPF Button which should be displayed with a png image which has transparent areas.
This is my xaml code, which works for displaying an image:
<Button HorizontalAlignment="Left" VerticalAlignment="Top" Panel.ZIndex="2" Background="Transparent" BorderBrush="Transparent">
<Image Width="1cm" Height="1cm" x:Name="LogoImageChangeButton_Image"/>
</Button>
This is the image. As you can see, it has transparent areas:
Unfortunately, wpf seems to repeat or assume pixels in the transparent areas for some unknown reason. Unfortunately, the effect looks quite glitchy so I cant describe it any better:
I have tried around with different stretch modes, all seemed to have this issue. Any help is appreciated.
This is how I set the image:
LogoImageChangeButton_Image.Source = BitmapHelper_Net.BitmapConverter.Convert(Properties.Resources.ChangeImageIcon);
and this is how I convert the bitmap to an imagesource:
public static BitmapImage Convert(Bitmap src)
{
MemoryStream ms = new MemoryStream();
((System.Drawing.Bitmap)src).Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage image = new BitmapImage();
image.BeginInit();
ms.Seek(0, SeekOrigin.Begin);
image.StreamSource = ms;
image.EndInit();
return image;
}

Do not use BMP as conversion format. It does not support transparency.
Use PNG, and set BitmapCacheOption.OnLoad to be able to dispose of the stream after EndInit.
public static BitmapImage Convert(Bitmap src)
{
var image = new BitmapImage();
using (var ms = new MemoryStream())
{
src.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = ms;
image.EndInit();
}
return image;
}

Related

BitmapImage from ByteArray/MemoryStream: Pixel format not supported

I am converting a BitmapImage from byte array, and it works fine, except with one specific .tif file. It gives an exception at EndInit():
The bitmap pixel format is unsupported. (Exception from HRESULT: 0x88982F80)
The code used to read the byte array to BitmapImage is:
public BitmapImage LoadImage(byte[] ImageData)
var image = new BitmapImage();
using (var mem = new MemoryStream(imageData))
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit(); //Exception here
}
image.Freeze();
return image;
}
What does this mean? The image opens just fine in windows, so it is not corrupted or anything. Is there a way to convert the image to supported pixel format, before the EndInit?

C# Removing transparancy from BitmapImage

i'm using a .png as a BitmapImage. The image is being used as a texture on a 3d model.
There is some transparancy in the image which i want to remove.
How do i remove it? (as efficiently as possible, using VisualBrush or System.Drawing.Image is not an option.)
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(fullPath, UriKind.Absolute);
bi.EndInit();
bi.Freeze();
ImageBrush br = new ImageBrush(bi) { Stretch = Stretch.Fill };
DiffuseMaterial mat = new DiffuseMaterial(br);

C#: Getting black image after initiliazing Image with Bitmapimage in WPF

Hi Developer,
I have an issue to display a bitmap image in my System.Windows.Controls.Image,
Am I using Pixelformat correctly?
I am able to display the image, but it shows me just a black rectangle.
I am trying to get the image from the Scanner.
Thank you very much in advance.
Bitmap b = new Bitmap(displayW, displayH, image.PixelFormat);
VCardScan.MainWindow.AppWindow.setImageSource(BitmapToImageSource(b));
public void setImageSource(BitmapImage bitmap)
{
CardBox.Source = bitmap;
}
BitmapImage BitmapToImageSource(Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
System.Console.WriteLine(bitmap.ToString());
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}

Unable to display Image created from Base64 BitmapImage

I want to display an Image in a StackPanel from a base64 string but it's not working correctly.
string base64encodedImage = el.Value;
byte[] imageData = Convert.FromBase64String(base64encodedImage);
Image imageSection = new Image();
BitmapImage image = new BitmapImage();
using (MemoryStream memStream = new MemoryStream(imageData))
{
image.BeginInit();
image.StreamSource = memStream;
image.EndInit();
}
imageSection.Source = image;
panel.Children.Add(imageSection);
Example Base64 Image:
iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==
The BitmapImage gets a width and height, the Image however does not and nothing is displayed, what am I doing wrong?
Refer to this post to find the correct way to get the BitmapImage from byte array.

Fill WPF rectangle with Image

I created new WPF control, added a Rectangle to it, and everyhing works alright, its drawn like it should be. But I just cant paint the rectangle with an actual Image.
BitmapImage bi = GetImage();
ImageBrush imgBrush= new ImageBrush(bi);
this.rectangle.Fill = imgBrush;
But this code just makes the rectangle transparent, except the stroke.
This is the GetImage() method:
BitmapImage bi;
using (MemoryStream ms = new MemoryStream())
{
bi = new BitmapImage();
bi.CacheOption = BitmapCacheOption.OnLoad;
texture.SaveAsPng(ms, texture.Width, texture.Height);
ms.Seek(0, SeekOrigin.Begin);
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
ms.Close();
}
return bi;
texture is an Texture2D class, that is made before this code.
If I return Bitmap insted of BitmapImage here and then save that Bitmap the picture is drawn correctly.
Thank you for your help
This is the correct way to convert Bitmap to BitmapImage:
using(MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
}
Thanks to "Pawel Lesnikowski", he posted the anwser in the following topic:
Load a WPF BitmapImage from a System.Drawing.Bitmap

Categories

Resources