Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase the resolution (dpi) of an image

Tags:

c#

image

bitmap

zpl

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.
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.
So... the image comes out small.

I need to increase the dpi of the image to print it at its 'real' size.

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. 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?

like image 695
jb. Avatar asked Dec 16 '22 21:12

jb.


1 Answers

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);
like image 81
Grzegorz W Avatar answered Dec 29 '22 05:12

Grzegorz W