更改图像对象的 DPI
Change DPI of Image object
我想将 Bitmap 的 VerticalResolution
和 HorizontalResolution 更改为固定值 300
。
我有一个 Windows 服务,它需要一些 TIFF 并执行一些与条形码相关的操作。除此之外,最后我从单页创建了一个多页 TIFF。
问题是原来的 DPI 总是 300 而结果是 96 DPI。
即使分辨率相同且文件大小不变(考虑到额外的页面),这似乎也是唯一相关的区别。
这是相关的,因为我需要在每个文件中使用 300 DPI。
这是我认为原因所在的代码,摘自此处:https://www.codeproject.com/Articles/16904/Save-images-into-a-multi-page-TIFF-file-or-add-ima
private Bitmap ConvertToBitonal(Bitmap original)
{
Bitmap source = null;
// If original bitmap is not already in 32 BPP, ARGB format, then convert
if (original.PixelFormat != PixelFormat.Format32bppArgb)
{
source = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb);
source.SetResolution(original.HorizontalResolution, original.VerticalResolution);
using (Graphics g = Graphics.FromImage(source))
{
g.DrawImageUnscaled(original, 0, 0);
}
}
else
{
source = original;
}
// some stuff here
// Create destination bitmap
Bitmap destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);
// other stuff
}
正在调试,看到指令前:
Bitmap destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);
位图的垂直分辨率为 300,水平分辨率为 300。
变成96x96后。
如何更改这些图像属性以获得 300 DPI 的图像?
使用 SetResolution 方法设置原始 Xdpi 和 Ydpi 已解决,新位图对象的默认 DPI 为 96x96,如下面的答案中所指出。
代码创建位图目标。位图的分辨率默认为 96dpi x 96 dpi。由于没有设置其他分辨率,输出文件具有默认分辨率...
您的问题的答案可以在参考手册中找到。
https://msdn.microsoft.com/en-us/library/system.drawing.bitmap(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.setresolution(v=vs.110).aspx
public void SetResolution(
float xDpi,
float yDpi
)
Sets the resolution for this Bitmap.
如果您理解从某处复制的代码,您会发现您的问题已经有了答案...
source.SetResolution(original.HorizontalResolution, original.VerticalResolution);
我想将 Bitmap 的 VerticalResolution
和 HorizontalResolution 更改为固定值 300
。
我有一个 Windows 服务,它需要一些 TIFF 并执行一些与条形码相关的操作。除此之外,最后我从单页创建了一个多页 TIFF。
问题是原来的 DPI 总是 300 而结果是 96 DPI。
即使分辨率相同且文件大小不变(考虑到额外的页面),这似乎也是唯一相关的区别。
这是相关的,因为我需要在每个文件中使用 300 DPI。
这是我认为原因所在的代码,摘自此处:https://www.codeproject.com/Articles/16904/Save-images-into-a-multi-page-TIFF-file-or-add-ima
private Bitmap ConvertToBitonal(Bitmap original)
{
Bitmap source = null;
// If original bitmap is not already in 32 BPP, ARGB format, then convert
if (original.PixelFormat != PixelFormat.Format32bppArgb)
{
source = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb);
source.SetResolution(original.HorizontalResolution, original.VerticalResolution);
using (Graphics g = Graphics.FromImage(source))
{
g.DrawImageUnscaled(original, 0, 0);
}
}
else
{
source = original;
}
// some stuff here
// Create destination bitmap
Bitmap destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);
// other stuff
}
正在调试,看到指令前:
Bitmap destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);
位图的垂直分辨率为 300,水平分辨率为 300。 变成96x96后。
如何更改这些图像属性以获得 300 DPI 的图像?
使用 SetResolution 方法设置原始 Xdpi 和 Ydpi 已解决,新位图对象的默认 DPI 为 96x96,如下面的答案中所指出。
代码创建位图目标。位图的分辨率默认为 96dpi x 96 dpi。由于没有设置其他分辨率,输出文件具有默认分辨率...
您的问题的答案可以在参考手册中找到。
https://msdn.microsoft.com/en-us/library/system.drawing.bitmap(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.setresolution(v=vs.110).aspx
public void SetResolution(
float xDpi,
float yDpi
)
Sets the resolution for this Bitmap.
如果您理解从某处复制的代码,您会发现您的问题已经有了答案...
source.SetResolution(original.HorizontalResolution, original.VerticalResolution);