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



这是我发布的第一个问题。所以请善待我,然而,关于如何改进我的提问以提高可读性的评论是非常受欢迎的。

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

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

例如,它首先使用Marshal.Copy().将一个简单的short数组(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);
}

然后我从"旋转"图像中读取原始数据。

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充满了{252252252,2,2,2,5,5,5];而不是预期的:[255255255,22,2,2,5.5,5]。

我试着填充背景,画一个矩形等等。我真的不知道是什么原因导致目标位图中的数据不包含源图像中的数据。图形的准确性是否受到影响?

在MSDN上,有一条评论指出:

PixelFormat48bppRGB、PixelFormat64bppARGB和PixelFormat64 bppPARGB每个颜色分量(通道)使用16位。GDI+版本1.0和1.1可以读取每个通道16位的图像,但这些图像被转换为每个通道8位的格式,用于处理、显示和保存。每个16位颜色通道都可以保存0到2^13范围内的值。

我敢打赌,这就是导致这里失去准确性的原因。

也许您可以选择Format24bppRgb,并且每个短片使用两个像素;当还将srcCols设置为其值的两倍时,它似乎为您的示例返回了正确的结果。

最新更新