图形未定义绘制到目标位图C#

graphics undefined drawing to the destination bitmap C#

这是我向 post 提出的第一个问题。所以请善待我,但是非常欢迎就如何改进我的提问以提高可读性发表评论。

我尝试使用图形旋转一组短裤。

我将一组短裤读取到图像中,使用图形旋转它,然后将其存储回一组短裤中。 但是我遇到图形处理程序没有按预期工作所以我剥离了我的代码,它看起来像这样:

它首先使用 Marshal.Copy() 将一个简单的短裤数组 (source48) 复制到 src 位图。

   short[] source48= new short[]{255,255,255,2,2,2,5,5,5];
   int srcCols=3,int srcRows=1;
   Drawing.Bitmap srcImage = new Drawing.Bitmap(srcCols,srcRows, System.Drawing.Imaging.PixelFormat.Format48bppRgb);   
   System.Drawing.Imaging.BitmapData data = srcImage.LockBits(
                new Drawing.Rectangle(0, 0, srcCols, srcRows),
                System.Drawing.Imaging.ImageLockMode.WriteOnly,
                System.Drawing.Imaging.PixelFormat.Format48bppRgb);
   // Copy the source buffer to the bitmap
   Marshal.Copy(source48, 0, data.Scan0, source48.Length);
   // Unlock the bitmap of the input image again.
   srcImage.UnlockBits(data);
   data = null;             

比它创建一个新的位图 "rotatedImage" 并使用图形用 "srcImage" 填充 "rotatedImage"(我现在跳过实际旋转)

  Drawing.Bitmap rotatedImage = new drawing.Bitmap(srcCols,srcRows,System.Drawing.Imaging.PixelFormat.Format48bppRgb);
  rotatedImage.SetResolution(srcImage.HorizontalResolution, srcImage.VerticalResolution);
  using (Drawing.Graphics g = Drawing.Graphics.FromImage(rotatedImage))
  {
        g.Clear(Drawing.Color.Black);
        g.DrawImage(srcImage, 0, 0);
  }

然后我从 "rotated"Image 中读取原始数据。

 data = rotatedImage.LockBits(
                new Drawing.Rectangle(0, 0, srcCols, srcRows),
                System.Drawing.Imaging.ImageLockMode.ReadOnly,
                System.Drawing.Imaging.PixelFormat.Format48bppRgb);
 // Copy the bulk from the output image bitmap to a 48bppRGB buffer
 short[] destination48 = new short[9];
 Marshal.Copy(data.Scan0, destination48, 0, destination48.Length);

不幸的是我发现 destination48 充满了 {252,252,252,2,2,2,5,5,5];而不是预期的:[255,255,255,2,2,2,5,5,5].

我尝试过填充背景、绘制矩形等。 我真的不知道目标位图中的数据不包含源图像中的数据的原因是什么。图形的准确性是否受到影响?

在 MSDN 上,there is a comment 指出:

PixelFormat48bppRGB, PixelFormat64bppARGB, and PixelFormat64bppPARGB use 16 bits per color component (channel). GDI+ version 1.0 and 1.1 can read 16-bits-per-channel images, but such images are converted to an 8-bits-per-channel format for processing, displaying, and saving. Each 16-bit color channel can hold a value in the range 0 through 2^13.

我敢打赌这就是导致此处准确性下降的原因。

也许你可以选择Format24bppRgb,每个短片使用两个像素;当还设置 srcCols 使其值加倍时,似乎 return 您的示例的正确结果。