【发布时间】: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 设置为图像的 HorizontalResolution/VerticalResolution 来解决它,反之亦然?
-
不,这些属性没有设置器。
-
@HansPassant 我厌倦了使用 g.DrawImage(bmpIn, 0, 0);但我没有工作我不知道问题的原因!为什么改变每个像素的位数会影响空间分辨率?