使用Marshal.Copy复制像素值后图像出现断层问题求助
图像复制时出现断层问题
[输入图片描述]
尝试将数据从一个Bitmap复制到另一个时,图像出现了断层现象。
以下是我从微软文档复制的代码,理论上应该可行,但实际无法正常运行:
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); }
更新:我用相关代码解决了问题,但仅适用于24位格式的.jpg图片。
内容的提问来源于stack exchange,提问作者Tirais
相关产品推荐
相关产品推荐

