简体   繁体   English

增加图像的分辨率(dpi)

[英]Increase the resolution (dpi) of an image

I'm not sure how feasible this will be without some thirdparty libraries, but here goes: 我不确定没有一些第三方库会有多可行,但是这里有:

I have an image, 450x900 in size, which im trying to print. 我有一个450x900的图像,我试图打印。
The problem is, the method I'm using to print is sending raw data to the printer. 问题是,我用来打印的方法是将原始数据发送到打印机。
The resolution of the image is 96dpix96dpi, the printer runs at 203dpi. 图像分辨率为96dpix96dpi,打印机运行速度为203dpi。
So... the image comes out small. 所以......图像很小。

I need to increase the dpi of the image to print it at its 'real' size. 我需要增加图像的dpi,以“真实”尺寸打印它。

Bitmap b0 = LoadBitmap();

//I need to rotate it because for some odd reason it prints backwards and upside down.
b0.RotateFlip(RotateFlipType.Rotate180FlipX);

//Set a new resolution, 203dpi
b0.SetResolution(203, 203);

//I need to save and reload the bitmap, because RotateFlip compresses it.
//(annoying as hell, took me ages to figure out why it wasn't working.)
Stream imgStream = new MemoryStream();
b0.Save(imgStream, ImageFormat.Bmp);
b0 = new Bitmap(imgStream);

//get my byte array
ImageConverter converter = new ImageConverter();
byte[] imageData = (byte[])converter.ConvertTo(b0, typeof(byte[]));

So, fairly straight forward. 所以,相当直接。 But SetResolution(...) doesn't actually seem to do anything. 但SetResolution(...)实际上似乎没有做任何事情。 The image prints exactly the same size, and the resultant byte array is exactly the same size. 图像打印的大小完全相同,结果字节数组的大小完全相同。

So I'm trying to work out what it is actually doing. 所以我想弄清楚它到底在做什么。
But I guess it would need to pad out all the image data with extra pixels to do what I want it to? 但我想它需要用额外的像素填充所有图像数据来做我想要的吗?

If this isn't a practical method, is there a simple stretch method or similar I can use to get the desired effect? 如果这不是一个实用的方法,是否有一个简单的拉伸方法或类似我可以用来获得所需的效果?

Why not scale your image to be bigger: 为什么不将图像缩放为更大:

        System.Drawing.Bitmap b0 = LoadBitmap();
        double scale = 203/96;
        int width = (int)(b0.Width * scale);
        int height = (int)(b0.Height * scale);
        System.Drawing.Bitmap bmpScaled = new System.Drawing.Bitmap(b0,width, height);

You have to understand that simply changing resolution (dpi) of a bitmap does nothing to its pixel content. 您必须明白,仅仅更改位图的分辨率(dpi)对其像素内容不起作用。 For bitmap "resolution" is only a metadata. 对于位图,“分辨率”仅是元数据。 What really matter is size in pixels. 真正重要的是像素大小。

So if you want your picture to be bigger in print you'll have to either change printer resolution or scale your image (for example, using Grzegorz's method ). 因此,如果您希望图片在打印时更大,则必须更改打印机分辨率或缩放图像(例如,使用Grzegorz的方法 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM