【问题标题】:The image is shifted when convert 8bpp to 24bpp将 8bpp 转换为 24bpp 时图像发生偏移
【发布时间】:2012-10-19 02:16:20
【问题描述】:

当我使用以下代码将位图从 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");
}


【问题讨论】:

  • DrawImageUnscaled() 并没有做每个人都希望它做的事情。请改用 DrawImage(Image, Rectangle) 重载。
  • 第一个 bmpIn.Save 应该是 bmpIn.Load 吗?
  • @HansPassant:这是否意味着您也可以通过将图形对象 DpiX/DpiY 设置为图像的 Horizo​​ntalResolution/VerticalResolution 来解决它,反之亦然?
  • 不,这些属性没有设置器。
  • @HansPassant 我厌倦了使用 g.DrawImage(bmpIn, 0, 0);但我没有工作我不知道问题的原因!为什么改变每个像素的位数会影响空间分辨率?

标签: c# image graphics


【解决方案1】:

以下代码对我有用:

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");
}

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

【讨论】:

    【解决方案2】:

    在您的函数中收到 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。正如您在 cmets 中所说的那样,我使用了 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");
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-18
      • 2019-12-03
      • 2011-09-15
      • 2013-02-25
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 2014-08-09
      • 2013-06-11
      相关资源
      最近更新 更多