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
Related
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);
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 am trying to append image to image source but after executing the code image is not displaying in my page.
Code:
Bitmap bmp = (Bitmap)data.img;
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
imgPhoto.Source = bi;
this is data.img specifications that I want to append to imhPhoto.Source.
After working around this issue, the solution for this question is:
call the function to get BitmapImage and save it in photo variable like this:
BitmapImage photo = byteToImage(byte[] buffer)
this functionto convert byte to BitmapImage
public BitmapImage byteToImage(byte[] buffer)
{
using(var ms = new MemoryStream(buffer))
{
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = ms;
image.EndInit();
}
return image;
}
finally, append converted photo to image source like this:
imgPhoto.Source = photo;
You can assign path like this.
//for App folder path
//var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)+"\\Image\\pic.jpg";
//for machine path
var path = #"C:\Users\User1\Pictures\pic.jpg";
Bitmap bmp = new Bitmap(path);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
imgPhoto.Source = bi;
If this solves your problem :
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(#"g:\screenshot.png");
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
bi.StreamSource = ms;
bi.EndInit();
imgPhoto.Source = bi;
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;
}));
I have this:
BitmapImage bi = new BitmapImage();
bi.CreateOptions = BitmapCreateOptions.None;
bi.UriSource = new Uri(url, UriKind.RelativeOrAbsolute);
,where url =www.xyz.com/abc.jpg
My question is "How to get the bi.PixelWidth and bi.PixelHeight?" because both are coming as zero. I want to resize big images in to thumbnails.
I have tried this one.
I am fairly new to WP7 development. I cam across this issue and is actually still trying to figure out how to do this.
But I could get it to work with WritableBitmap like so :
Uri uri = new Uri("/image.jpg", UriKind.Relative);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.CreateOptions = BitmapCreateOptions.None;
bitmapImage.UriSource = uri;
WriteableBitmap img = new WriteableBitmap(bitmapImage);
using (MemoryStream ms = new MemoryStream())
{
// write an image into the stream
Extensions.SaveJpeg(img, ms, img.PixelWidth, img.PixelHeight, 0, 100);
byte[] byteArray = ms.ToArray();
}