wyvern010
Topic Author
Posts: 14
Joined: 18 Apr 2019, 13:41

BitmapImage.Create Question

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,
Image
Result Image (black border is from using snipping tool)
Image


   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;
        }

Tags:
 
User avatar
jsantos
Site Admin
Posts: 2538
Joined: 20 Jan 2012, 17:18
Contact:

Re: BitmapImage.Create Question

03 Jul 2019, 17:42

when the returned ImageSource is rendered, the image is upside down and parts of the image have shifted.
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.
 
wyvern010
Topic Author
Posts: 14
Joined: 18 Apr 2019, 13:41

Re: BitmapImage.Create Question

03 Jul 2019, 20:33

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;
                    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);
                    
 
User avatar
jsantos
Site Admin
Posts: 2538
Joined: 20 Jan 2012, 17:18
Contact:

Re: BitmapImage.Create Question

09 Jul 2019, 20:37

Thanks for sharing your solution!

Who is online

Users browsing this forum: asusralis and 4 guests