BitmapImage.Create Question
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;
}
Re: BitmapImage.Create Question
Shifting is probably happening because image pitch is not correctly handled. Regarding upside down, you need to invert your byte array. Not sure if we are following WPF convention here about what's up or down, I will check it later.when the returned ImageSource is rendered, the image is upside down and parts of the image have shifted.
Re: BitmapImage.Create Question
Array.Reverse(barray, 0, barray.Length);
Fixed the isue of Upside down and shifting.
And also the RGB -> BGR.
Now the image is just 180 degrees rotated allong the Y-axis.
Edit: Everything fixed;
Fixed the isue of Upside down and shifting.
And also the RGB -> BGR.
Now the image is just 180 degrees rotated allong the Y-axis.
Edit: Everything fixed;
Code: Select all
Bitmap newbmp = Convert(new Bitmap(person.photo.file.ContentStream));
//first rotate y-axis 180;
newbmp.RotateFlip(RotateFlipType.Rotate180FlipY);
// invert byte array this also fixed the RGB -> BRG8.
byte[] barray = ToByteArray(newbmp, ImageFormat.Bmp);
Array.Reverse(barray, 0, barray.Length);
//.......
return BitmapImage.Create(newbmp.Width, newbmp.Height, newbmp.HorizontalResolution, newbmp.VerticalResolution, barray, stride, BitmapSource.Format.RGB8);
Re: BitmapImage.Create Question
Thanks for sharing your solution!
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 75 guests