【发布时间】:2016-09-21 20:11:30
【问题描述】:
这是我发布的第一个问题。所以请善待我,但是关于如何改进我的提问以提高可读性的 cmets 非常受欢迎。
我尝试使用图形旋转一组短裤。
我将一组短裤读取到图像中,使用图形对其进行旋转,然后将其存储回短裤数组中。 但是我遇到了图形处理程序没有按预期工作,所以我将我的代码剥离到它看起来像这样:
它首先使用 Marshal.Copy() 将一个简单的 short 数组 (source48) 复制到 src Bitmap。
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);
}
然后我从“旋转”图像中读取 rawData。
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]。
我尝试填充背景、绘制矩形等。 我真的不知道目标位图中的数据不包含源图像中的数据的原因是什么。图形的准确性是否受到影响?
【问题讨论】:
-
如果先保存
Bitmap并检查图像会怎样? -
另外,
Format48bppRgb * srcCols * srcRows!=sizeof(short) * srcCols * srcRows -
Marshall.copy 需要以字节为单位的大小。试试destination.lengrh * sizeof(短)
-
@JeroenvanLangen 如果我尝试得到一个超出范围的异常。仍然发现一些未定义的行为。如果我将 short 数组更改为: [1,0,0,2,0,0,3,0,0,4,0,0,5,0,0,6,0,0] 和 srcRows=3 & src 列=2。结果是 [0 0 0 2 0 0 2 0 0 0 0 0 5 0 0 5 0 0]。我现在很困惑。
标签: c# graphics bitmap drawing