使用给定的DPI将DrawingVisual保存到图像文件 [英] Saving DrawingVisual to an image file with a given DPI

查看:58
本文介绍了使用给定的DPI将DrawingVisual保存到图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,我想将Canvas保存到具有正确DPI值的文件中.画布大小是实际的物理大小,例如.20x10厘米,在300 DPI下,所以是2362x1181.

I have got a WPF application, and I would like to save a Canvas to a file with a correct DPI value. The Canvas size is the real physical size, eg. 20x10 cm, at 300 DPI, so it's 2362x1181.

我用DrawingVisual绘制图像和形状,然后创建一个RenderTargetBitmap.rtb的大小是Canvas的大小.dpiX和dpiY为96.只有使用96 DPI才能获得正确的图像分辨率.当我将其设置为300时,画布将变为高档,并裁剪为2362x1181.因此,这不是很好.我试图通过dpi系数修改画布的宽度和高度值,但也没有用.

I draw images and shapes with DrawingVisual, then I create a RenderTargetBitmap. The size of the rtb is the size of the Canvas. The dpiX and dpiY are 96. I got correct image resolution only with 96 DPI. When I set it to 300, the canvas become upscale, and cropped to 2362x1181. So, it's not good. I tried to modify the canvas width and height value by dpi factor, but didn't work either.

在RenderTargetBitmap之后,我使用BitmapEncoder,BitmapFrame和BinaryWriter.请参阅下面的代码.效果很好,但是图像DPI值为96.我已经阅读了很多有关读取DPI,使用SetResolution保存图像的主题,但是我不想降低质量,我不想读取文件并重新保存它,我不想更改像素的宽度/高度等.

After the RenderTargetBitmap, I use BitmapEncoder, BitmapFrame, and BinaryWriter. See code below. Working great, but the image DPI value will be 96. I've read a tons of topics about reading DPI, resaving image, using SetResolution, but I don't want to loose quality, I don't want to read file and resave it, I don't want to change pixel width/height, etc.

我真的只是想保存带有给定DPI的DrawingVisual.我可以为"X Resolution"(uint = 282)和"Y Resoulution"(uint = 283)写EXIF数据,但这并没有影响Image DPI设置,例如.Photoshop将读取96,而不是300.

I just really want to save a DrawingVisual with a given DPI. I could write EXIF data for "X Resolution" (uint=282) and "Y Resoulution" (uint=283), but that didn't affect the Image DPI settings, so eg. Photoshop will read 96, not 300.

BitmapEncoder encoder = new JpegBitmapEncoder();
BitmapFrame bFrame = BitmapFrame.Create(rtb, null, meta, icc);
encoder.Frames.Add(bFrame);

using (var stream = new MemoryStream())
{
encoder.Save(stream);
byte[] imageData = stream.ToArray();
using (FileStream fs = new FileStream(path, ...)
{
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(imageData);
bw.Close();
}
}

推荐答案

毕竟,解决方案似乎是Bitmap SetResolution.我测试了它,它看起来像不影响JpegBitmapEncoder()之后的图像质量!并且图像分辨率保持不变,保留元数据,只有DPI会改变!

It seems the solution is the Bitmap SetResolution, after all. I tested it, and it looks like not affect the image quality after the JpegBitmapEncoder()! And the image resolution is untouched, keep the metadata, only the DPI will change!

帮助了此文档: 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆