You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 16:24:55