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;
Related
I am able to download (retrieve) image from Mysql Database in WPF in C# by this piece of code.
This code i have copy from this https://www.experts-exchange.com/questions/25096053/Retrieve-images-in-C-WPF-Application-from-SQL-Server-Database.html
Website.
But i don't know how this code work line by line. If anyone who have knowledge about this please help.
Code is here.
string query = "SELECT image_data from image_table WHERE image_id=22";
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
Byte[] bindata = (Byte[])dataReader["image_data"];
MemoryStream strm = new MemoryStream();
strm.Write(bindata, 0, bindata.Length);
strm.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(strm);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
download.Source = bi;
}
i am able to retrieve image from database in wpf in c# with this code. The only problem with this code is it retrieve only one image ata a time. Before using this code add System.Drawing.Imaging. library in your code.
BitmapImage bi = new BitmapImage();
System.Drawing.Image img;
MemoryStream strm = new MemoryStream();
strm.Write(bindata, 0, bindata.Length);
strm.Position = 0;
img = System.Drawing.Image.FromStream(strm);
bi.BeginInit();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
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;
}
}
In my wpf application I get a byte array of a bmp file.
I want to create a new System.Windows.Media.Imaging.BitmapImage.
I created MemoryStream from the byte array, but it doesn't work with SetSource.
Any suggestions ?
Add reference:
using System.IO;
Use the following code.
MemoryStream ms = new MemoryStream(imageArray);
Image image = Image.FromStream(ms);
For WPF
public static BitmapImage GetBitmapImage(byte[] imageArray)
{
using (var stream = new MemoryStream(imageArray))
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
return bitmapImage;
}
}
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 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();
}