2

当我使用以下代码将位图从 8bpp 转换为 24bpp 时,结果图像发生了偏移,但我无法弄清楚原因,谁能帮忙?

private static Bitmap ConvertTo24(Bitmap bmpIn)
{
    bmpIn.Save(@"F:\sara1.bmp");
    Bitmap converted = new Bitmap(bmpIn.Width, bmpIn.Height, PixelFormat.Format24bppRgb);
    using (Graphics g = Graphics.FromImage(converted))
    {
         // Prevent DPI conversion
         g.PageUnit = GraphicsUnit.Pixel;
         // Draw the image
         g.DrawImageUnscaled(bmpIn, 0, 0);
         // g.DrawImage(bmpIn, 0, 0);
    }
    converted.Save(@"F:\sara2.bmp");
}

在此处输入图像描述


在此处输入图像描述

4

2 回答 2

2

以下代码对我有用:

private static Bitmap ConvertTo24(Bitmap bmpIn)
{
    bmpIn.Save(@"F:\sara1.bmp");
    Bitmap converted = new Bitmap(bmpIn.Width, bmpIn.Height, PixelFormat.Format24bppRgb);
    using (Graphics g = Graphics.FromImage(converted))
    {
        // Prevent DPI conversion
        g.PageUnit = GraphicsUnit.Pixel;
        // Draw the image
        // g.DrawImageUnscaled(bmpIn, 0, 0);
        g.DrawImage(bmpIn, 0, 0, converted.Width, converted.Height);
    }
    converted.Save(@"F:\sara2.bmp");
}

提供宽度和高度会重新缩放图像。

于 2012-11-05T21:20:24.873 回答
1

bmpIn在您的功能中收到它之前,您可能会遇到问题。您可以在放入 8bpp 图像后测试以下(更好的)代码f:\sara1.bmp(只是为了确保在转换之前没有移动 8bpp 图像)。

private static Bitmap ConvertTo24(Bitmap bmpIn)
{
    //Bitmap bmpIn;
    bmpIn = new Bitmap(@"f:\sara1.bmp");
    Bitmap converted = new Bitmap(bmpIn.Width, bmpIn.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    using (Graphics g = Graphics.FromImage(converted))
    {
         g.PageUnit = GraphicsUnit.Pixel;
         Rectangle rng = new Rectangle(new Point(0, 0), bmpIn.Size);
         g.DrawImage(bmpIn, rng);  // use this instead of following
    }
    converted.Save(@"F:\sara2.bmp");
}

希望它工作正常 4 u。我使用了 DrawImage(Image img, Rectangle rng) 正如你在评论中所说的那样

回答完毕。以下为说明。您的代码(已编辑。按原样使用并遵循评论)

private static Bitmap ConvertTo24(Bitmap bmpIn)
{
    bmpIn.Save(@"F:\sara1.bmp");
    return;
    // After you have called this function. Go open your image f:\sara1.bmp from
    //hrad disk-- I doubt you would find it shifted here even before conversion
    Bitmap converted = new Bitmap(bmpIn.Width, bmpIn.Height, PixelFormat.Format24bppRgb);
    using (Graphics g = Graphics.FromImage(converted))
    {
         // Prevent DPI conversion
         g.PageUnit = GraphicsUnit.Pixel;
         // Draw the image
         g.DrawImageUnscaled(bmpIn, 0, 0);
         // g.DrawImage(bmpIn, 0, 0);
    }
    converted.Save(@"F:\sara2.bmp");
}

再次你的代码(我只是忽略了你收到的 8bpp 位图并直接从 f 驱动器中获取它,假设你会在运行应用程序之前或至少在调用此函数之前放置它)

以下代码对我来说也很好用。

private static Bitmap ConvertTo24(Bitmap bmpIn)
{
    Bitmap bmpIn = new Bitmap(@"f:\sara1.bmp");
    Bitmap converted = new Bitmap(bmpIn.Width, bmpIn.Height, PixelFormat.Format24bppRgb);
    using (Graphics g = Graphics.FromImage(converted))
    {
         // Prevent DPI conversion
         g.PageUnit = GraphicsUnit.Pixel;
         // Draw the image
         g.DrawImageUnscaled(bmpIn, 0, 0);
         // g.DrawImage(bmpIn, 0, 0);
    }
    converted.Save(@"F:\sara2.bmp");
}
于 2012-11-05T20:41:32.253 回答