BitmapImage.Create Question
Posted: 02 Jul 2019, 16:59
Hello, im loading images from a database, that database provides me these images with a stream or a byte[].
I got to the point that i can use BitmapImage.Create without crashes.
now the weird thing is:
when the returned ImageSource is rendered, the image is upside down and parts of the image have shifted.
I saved the file after all conversion is done, and that bmp looks like it should.
Source Image,

Result Image (black border is from using snipping tool)

I got to the point that i can use BitmapImage.Create without crashes.
now the weird thing is:
when the returned ImageSource is rendered, the image is upside down and parts of the image have shifted.
I saved the file after all conversion is done, and that bmp looks like it should.
Source Image,

Result Image (black border is from using snipping tool)

Code: Select all
public ImageSource Photo
{
get
{
if (person != null && person.photo != null && person.photo.file != null)
{
Bitmap newbmp = Convert(new Bitmap(person.photo.file.ContentStream));
byte[] barray = ToByteArray(newbmp, ImageFormat.Bmp);
/*
* File.WriteAllBytes("D://Test.bmp",barray);
*/
return BitmapImage.Create(newbmp.Width, newbmp.Height, newbmp.HorizontalResolution, newbmp.VerticalResolution, barray,newbmp.Width * 3, BitmapSource.Format.BGR8);
}
return null;
}
set
{
OnPropertyChanged();
}
}
public static byte[] ToByteArray(Bitmap image, ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms,format);
return ms.ToArray();
}
}
private Bitmap Convert(Bitmap orig)
{
Bitmap clone = new Bitmap(orig.Width, orig.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics gr = Graphics.FromImage(clone))
{
gr.DrawImage(orig, new Rectangle(0,0,clone.Width,clone.Height));
}
return clone;
}