sabisabi
sabisabi

Reputation: 1511

Using .DrawToBitmap - how to change resolution of image?

Im using DrawToBitmap to save some labels as image. I would like to know how to change the resolution of these images, is there a way? Say I have a label with a text and I want to render it to an image file (not posting the full code):

this.label1 = new System.Windows.Forms.Label();
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Baskerville Old Face", 36F);
//...
this.label1.Size = new System.Drawing.Size(161, 54);
this.label1.Text = "Output";
//...

//save image:
Bitmap image = new Bitmap(161, 54);
this.label1.DrawToBitmap(image, this.label1.ClientRectangle);
image.Save(@"C:\image.jpg");

This is working fine, I will get something like this:

Resultant image

The resolution is ok, but is it possible to increase it? When I zoom into this image a little, I can see the individual pixels as large blocks:

Zoomed resultant image

I know that's normal, because its not a vector graphic and that's fine. I just would like to change it somehow, so that you can zoom in further before seeing individual pixels as large blocks. Any ideas?

Thank you.

Edit: If Im using only black/white images - is it maybe better to save the image as png or gif?

Upvotes: 4

Views: 3902

Answers (2)

DanielShi
DanielShi

Reputation: 23

you can achieve this by increasing the value of second parameter of System.Drawing.Font.

this.label1.Font = new System.Drawing.Font("Baskerville Old Face", 1000F);

Upvotes: 0

Gregor Primar
Gregor Primar

Reputation: 6805

Something like this will do the job, just increase font size used from label:

    Bitmap CreateBitmapImage(string text, Font textFont, SolidBrush textBrush)
    {
        Bitmap bitmap = new Bitmap(1, 1);
        Graphics graphics = Graphics.FromImage(bitmap);
        int intWidth = (int)graphics.MeasureString(text, textFont).Width;
        int intHeight = (int)graphics.MeasureString(text, textFont).Height;
        bitmap = new Bitmap(bitmap, new Size(intWidth, intHeight));
        graphics = Graphics.FromImage(bitmap);
        graphics.Clear(Color.White);
        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        graphics.DrawString(text, textFont, textBrush,0,0);
        graphics.Flush();
        return (bitmap);
    }

Upvotes: 1

Related Questions