[英]RotateFlip bug when using a 1Bpp image. C# What can I do?
我正在尝试使用1Bpp PixelIndex旋转位图,但是我发现它存在问题。 当您尝试进行一些旋转时,图像的左侧会出现黑线。 做一些研究,我发现这是一个错误,但可能不会得到解决。
我尝试了另一种旋转位图的方法(包括代码):
Bitmap returnBitmap = new Bitmap(lBitmap.Width, lBitmap.Height);
Graphics g = Graphics.FromImage(returnBitmap);
g.TranslateTransform((float)lBitmap.Width / 2, (float)lBitmap.Height / 2);
g.RotateTransform(180);
g.TranslateTransform(-(float)lBitmap.Width / 2, -(float)lBitmap.Height / 2);
g.DrawImage(lBitmap, new Point(0, 0));
mIsRotated = true;
但是我的问题是,旋转180º后图像失去清晰度。
还有其他旋转方式吗?
对不起,我很抱歉。
如果有人在这里遇到同样的问题,我找到了解决方案。 我不能使用Bitmap.RotateFlip,因为它会生成黑线,因此我尝试了上面的代码。 在180º时,我的图像失去了一些清晰度,但是使用-180º解决了该问题。
我从图标和光标提取单色位图时遇到了这个问题。
翻转不旋转。 这样会更好:
Bitmap returnBitmap = new Bitmap(lBitmap.Width, lBitmap.Height);
Graphics g = Graphics.FromImage(returnBitmap);
g.TranslateTransform((float)lBitmap.Width / 2, (float)lBitmap.Height / 2);
// Mirror instead of rotate
g.ScaleTransform(1,-1);
g.TranslateTransform(-(float)lBitmap.Width / 2, -(float)lBitmap.Height / 2);
g.DrawImage(lBitmap, new Point(0, 0));
mIsRotated = true;
但是,生成的位图将不会是1bpp
该函数利用行是32位对齐的事实来就地翻转单色位图:
/// <summary>
/// Vertically flips a monochrome bitmap in-place
/// </summary>
/// <param name="bmp">Monochrome bitmap to flip</param>
public static void RotateNoneFlipYMono(Bitmap bmp)
{
if (bmp == null || bmp.PixelFormat != PixelFormat.Format1bppIndexed)
throw new InvalidValueException();
var height = bmp.Height;
var width = bmp.Width;
// width in dwords
var stride = (width + 31) >> 5;
// total image size
var size = stride * height;
// alloc storage for pixels
var bytes = new int[size];
// get image pixels
var rect = new Rectangle(Point.Empty, bmp.Size);
var bd = bmp.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);
Marshal.Copy(bd.Scan0, bytes, 0, size);
// flip by swapping dwords
int halfSize = size >> 1;
for (int y1 = 0, y2 = size - stride; y1 < halfSize; y1 += stride, y2 -= stride)
{
int end = y1 + stride;
for (int x1 = y1, x2 = y2; x1 < end; x1++, x2++)
{
bytes[x1] ^= bytes[x2];
bytes[x2] ^= bytes[x1];
bytes[x1] ^= bytes[x2];
}
}
// copy pixels back
Marshal.Copy(bytes, 0, bd.Scan0, size);
bmp.UnlockBits(bd);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.