c# - 8bpp 转换为 24bpp 时图像发生偏移

标签 c# image graphics

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

enter image description here


enter image description here

最佳答案

以下代码对我有用:

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

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

关于c# - 8bpp 转换为 24bpp 时图像发生偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13127171/

相关文章:

math - 获取任意二维三角形的扫描线

java - 为什么一旦 run() 方法中的 if 语句为 true,我就不能再使用 keyPressed 了?

c# - Silverlight AccordionItem HeaderTemplate 中的超链接

c# - ASP.NET Core MVC 混合路由/FromBody 模型绑定(bind)和验证

c# - NLog 跟踪目标仅适用于跟踪和调试级别,不适用于信息、警告、错误或致命级别

css - 在加载 CSS 和 JS 文件时显示加载栏或动画 GIF?

c# - 将日期时间插入并更新到 SQL 数据库中

python - 在 Python 中使用 Reportlab 的图像纵横比

jquery - 关于性能jQuery的问题

python - Sklearnplot_tree图太小