0

enter image description here

When trying to copy data from one Bitmap to another, for some reason the image looks torn.

This is the code I copied from microsoft documentation. And it should work, but it doesn't.

public void DrawTextures()
        {
            Bitmap bmp = new("D:\\Photo.jpg");
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bmp.PixelFormat)
            IntPtr ptr = bmpData.Scan0;
            int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
            byte[] rgbValues = new byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
            bmp.UnlockBits(bmpData);
            PictureBox pictureBox = new()
            {
                BorderStyle = BorderStyle.FixedSingle,
                BackgroundImageLayout = ImageLayout.Stretch,
                Image = bmp,
                Width = 2048,
                Height = 2048
            };
            mainFlowPanel.Controls.Add(pictureBox);
        }

Update: I solved the problem with this code but only for .jpg 24 bit

Tirais
  • 7
  • 3
  • The code posted here makes no sense and cannot work -- You need to revisit the definition of `Stride`. Read the note here: [Analyze colors of an Image](https://stackoverflow.com/a/59102380/7444103) – Jimi Aug 19 '23 at 23:45
  • @Jimi In my case (I'm working with .png 2048x2048 and 256x256) my stride is equal to BitmapData.Stride. Changing them does not affect the result. But I'm starting to guess that it's the color conversion that's the issue. I haven't figured out what it is yet. – Tirais Aug 20 '23 at 07:50
  • Well, what is this: `int atlasDataStride = atlasData.Height * 4;`? This has nothing to do with the Bitmap's `Stride` (ScanLine). Read the notes I've linked -- This code, as mentioned, doesn't make sense and cannot work. So at least post some real code, without any variables parachuted into the method from nowhere (the only parameter, `numberInAtlas` is never used, the method tries to return `void`, when it's not how it's declared, etc.) – Jimi Aug 20 '23 at 08:35
  • @Jimi I've changed the code in the question. And indeed the problem is partially solved. But only after changing .png to .jpg – Tirais Aug 20 '23 at 09:01
  • There's an entire *chapter* dedicated to the Stride in that Q&A: **Important notes about the Stride**. You really need to read that. Also, as described there, when you perform this kind of operations, you either calculate the actual Stride, given that Bitmaps are always aligned to 4 bytes in memory, or you copy the source image to a 32bit ARGB image beforehand, then create a destination image of the same type. Otherwise, something always goes wrong – Jimi Aug 20 '23 at 09:17
  • BTW, the new code you have posted probably make less sense than the code you had before. You're just copying the bitmap data over itself – Jimi Aug 20 '23 at 09:22
  • @Jimi Yeah, it doesn't make sense. But I needed the copying process to be error-free in the first place. And I had that article earlier, as soon as you sent it. But I couldn't find anything that would help me. In my case there was really no difference between my stride and the one calculated in the class instance. Since it was used only for calculating the array length and always matched that of the instance. – Tirais Aug 20 '23 at 12:39

0 Answers0