I do have some problems to display an image (uEye-Cam) in a wpf-Image form. The displayed image is completely white. Below is my used code:
//Get Cam Bitmap Image
var cam = new uEye.Camera();
cam.Init();
cam.Memory.Allocate();
cam.Acquisition.Capture(uEye.Defines.DeviceParameter.DontWait);
Int32 s32MemId;
cam.Memory.GetActive(out s32MemId);
cam.Memory.Lock(s32MemId);
Bitmap bitmap;
cam.Memory.ToBitmap(s32MemId, out bitmap);
//WPF Image control
var image1 = new System.Windows.Controls.Image();
//convert System.Drawing.Image to WPF image
var bmp = new System.Drawing.Bitmap(bitmap);
IntPtr hBitmap = bmp.GetHbitmap();
System.Windows.Media.ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
image1.Source = wpfBitmap;
image1.Stretch = System.Windows.Media.Stretch.UniformToFill;
image1.Visibility = Visibility.Visible;
DeleteObject(hBitmap);
Is there anything wrong with the image1 properties maybe or..?
Thanks
WPF bitmap to image conversion shows a black image only
I've had good results with converting every new frame to a bitmap as in the question:
Bitmap bmp;
cam.Memory.ToBitmap(s32MemId, out bmp);
And then converting this to a BitmapImage using:
BitmapImage bitmapImage = null;
using (System.IO.MemoryStream memory = new System.IO.MemoryStream())
{
bitmapImage = new BitmapImage();
bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
}
And then locking and freezing (as in How do you pass a BitmapImage from a background thread to the UI thread in WPF?) the BitmapImage to set it as the source of the Image control using:
Object tempLock = new Object();
BitmapImage lastBitmapImage = null;
lock (tempLock)
{
lastBitmapImage = bitmapImage.Clone();
}
lastBitmapImage.Freeze();
Finally, the BitmapImage is set as the souce of the Image control with:
this.Dispatcher.Invoke(new Action(() =>
{
imageDisplay.Source = lastBitmapImage;
}));
Related
Using the .NET framework, when retrieving the image from the clipboard by converting the IRandomAccessStreamReference, the transparency on .PNG images is lost. How can i prevent this?
var content = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
if (content.Contains(StandardDataFormats.Bitmap))
{
IRandomAccessStreamReference imageReceived = await content.GetBitmapAsync();
BitmapImage bitmap = null;
using (IRandomAccessStreamWithContentType imageStream = await imageReceived.OpenReadAsync())
{
bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = imageStream.AsStream();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
bitmap.Freeze();
}
}
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;
}
}
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.
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
I had set up to access an embedded resource and return an System.Drawing.Image but i cant use this to set the background of a canvas.
Could someone please show me how to access an embedded image file and create a System.Windows.Controls.Image. Code I have so far is :
public static Image Load(Type classType, string resourcePath)
{
Assembly asm = Assembly.GetAssembly(classType);
Stream imgStream = asm.GetManifestResourceStream(resourcePath);
Image img = Image.FromStream(imgStream);
imgStream.Close();
return img;
}
Please let me know if you require anymore information
If you don't specifically need to use a `System.Drawing.Image', then you could try something like:
Assembly asm = Assembly.GetCallingAssembly();
var res = asm.GetManifestResourceNames();
Stream imgStream = asm.GetManifestResourceStream("path.to.resource");
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = imgStream;
image.EndInit();
ImageBrush brush = new ImageBrush();
brush.ImageSource = image;
imageCanvas.Background = brush;
Bitmap bm = new Bitmap(#"....");
img = bm;