C# 使用1Bpp图像时出现RotateFlip错误。我能做什么?

C# 使用1Bpp图像时出现RotateFlip错误。我能做什么?,c#,bitmap,C#,Bitmap,我正在尝试使用1Bpp PixelIndex旋转位图,但我发现它有漏洞。当您尝试进行某些旋转时,图像左侧将出现一条黑线。做一些研究,我发现这是一个错误,但可能不会被修复 我尝试了另一种旋转位图的方法(包括代码): 但我这里的问题是,图像旋转180º时会丢失清晰度 还有其他旋转方式吗 如果我不够清楚,我很抱歉。如果有人在这里结束时遇到同样的问题,我找到了解决办法。我无法使用Bitmap.RotateFlip,因为它生成了一条黑线,所以我尝试使用上面的代码。使用180º时,我的图像失去了一些清晰度,

我正在尝试使用1Bpp PixelIndex旋转位图,但我发现它有漏洞。当您尝试进行某些旋转时,图像左侧将出现一条黑线。做一些研究,我发现这是一个错误,但可能不会被修复

我尝试了另一种旋转位图的方法(包括代码):

但我这里的问题是,图像旋转180º时会丢失清晰度

还有其他旋转方式吗


如果我不够清楚,我很抱歉。

如果有人在这里结束时遇到同样的问题,我找到了解决办法。我无法使用Bitmap.RotateFlip,因为它生成了一条黑线,所以我尝试使用上面的代码。使用180º时,我的图像失去了一些清晰度,但使用-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);
}
//
///垂直翻转到位的单色位图
/// 
///要翻转的单色位图
公共静态void RotateNoneFlipYMono(位图bmp)
{
if(bmp==null | | bmp.PixelFormat!=PixelFormat.Format1bppIndexed)
抛出新的InvalidValueException();
var高度=bmp.高度;
var-width=bmp.width;
//dwords中的宽度
变量步长=(宽度+31)>>5;
//总图像大小
变量大小=步幅*高度;
//像素的alloc存储
var字节=新整数[大小];
//获取图像像素
var rect=新矩形(Point.Empty,bmp.Size);
var bd=bmp.LockBits(rect、ImageLockMode.WriteOnly、PixelFormat.format1bpindexed);
Marshal.Copy(bd.Scan0,字节,0,大小);
//通过交换DWORD进行翻转
int halfSize=大小>>1;
对于(int y1=0,y2=尺寸-步幅;y1<半尺寸;y1+=步幅,y2-=步幅)
{
int end=y1+步幅;
对于(int-x1=y1,x2=y2;x1
我在从图标和光标提取单色位图时遇到了这个问题

翻转不是旋转。这样会更好:

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);
}
//
///垂直翻转到位的单色位图
/// 
///要翻转的单色位图
公共静态void RotateNoneFlipYMono(位图bmp)
{
if(bmp==null | | bmp.PixelFormat!=PixelFormat.Format1bppIndexed)
抛出新的InvalidValueException();
var高度=bmp.高度;
var-width=bmp.width;
//dwords中的宽度
变量步长=(宽度+31)>>5;
//总图像大小
变量大小=步幅*高度;
//像素的alloc存储
var字节=新整数[大小];
//获取图像像素
var rect=新矩形(Point.Empty,bmp.Size);
var bd=bmp.LockBits(rect、ImageLockMode.WriteOnly、PixelFormat.format1bpindexed);
Marshal.Copy(bd.Scan0,字节,0,大小);
//通过交换DWORD进行翻转
int halfSize=大小>>1;
对于(int y1=0,y2=尺寸-步幅;y1<半尺寸;y1+=步幅,y2-=步幅)
{
int end=y1+步幅;
对于(int-x1=y1,x2=y2;x1
这些都是常见的插值工件,没有明显的原因说明它们为什么会出现在代码段中。您需要在文件共享服务上提供位图文件以获得帮助。在这里,您可以下载我正在使用的图像:1 bpp只提供2个调色板(背景和前景),不留任何插值(尽管90度、180度和270度旋转或翻转根本不需要插值)。尝试将图像转换为完整的RGB,然后旋转,如果可以的话。由于图像的大小,我必须使用1Bpp。你能添加一个原始代码,给你一条黑线吗?这些是常见的插值伪影,没有明显的原因从片段中出现。您需要在文件共享服务上提供位图文件以获得帮助。在这里,您可以下载我正在使用的图像:1 bpp只提供2个调色板(背景和前景),不留任何插值(尽管90度、180度和270度旋转或翻转根本不需要插值)。尝试将图像转换为完整的RGB,然后旋转,如果可以的话。由于图像的大小,我必须使用1Bpp。你能添加一个原始代码,给你一条黑线吗?