Windows Develop Bookmark and Share   
 index > Windows Forms General > Problem rendering metafile text onto a bitmap.
 

Problem rendering metafile text onto a bitmap.

I'm having problems with the quality of text from an enhanced metafile when it is rendered onto a System.Drawing.Bitmap.

If I draw it directly onto a form's surface, there is no problem, When drawn first onto a bitmap (and the bitmap then drawn onto the form) the text is blocky and very poor in quality.

I have noticed in the past that if a new Bitmap is created that is not first filled witha background color, then text written to it with Graphics.DrawString(...) has what looks like a similar quality issue. I guess this is because the anti-aliasing code needs a background color to work against. I can't see why this would be an issue with a metafile though.

Below is some C# code that shows what I mean.

To create a metafile to test this with, I used a single slide PowerPoint presentation with some text located at the top left of the slide,which was savedas a.emf in the Save As dialog.

The code assumes a standard Windows Forms appwith an OpenFileDialog control named 'openFileDialog1' and one button named 'loadButton' which has its Click handler connected to 'loadButton_Click'. The form should have a Size of about (800,800). Also needed is a using System.Drawing.Imaging directive.

The output is four images.

Top left is the metafile rendered directly onto the form.

Top right is the metafile rendered onto a bitmap.

Bottom left is text (not from the metafile) rendered onto a newly created but unfilled bitmap.

Bottom right is text (not from the metafile) rendered onto a newly created bitmap that has been filled with white, before drawing the string.

What I see is that the top left text looks fine, the top right is blocky, the bottom left is blocky and the bottom right is OK.

Any ideas why the metafile doesn't render properly when drawn on a bitmap? And any solutions?

Sorry for the long post. Any help appreciated. The answer might be obvious (I hope), but I'm really stuck on this one.

Chris.

Code Snippet

bool loaded;

Metafile metafile;

Bitmap metafileBitmap;

Bitmap textBitmapBlank;

Bitmap textBitmapFilled;

private void loadButton_Click(object sender, EventArgs e)

{

if (openFileDialog1.ShowDialog() == DialogResult.OK)

{

metafile = new Metafile(openFileDialog1.FileName);

loaded = true;

makeBitmaps();

Invalidate();

}

}

private void makeBitmaps()

{

metafileBitmap = new Bitmap(300, 300, PixelFormat.Format32bppArgb);

textBitmapBlank = new Bitmap(300, 300, PixelFormat.Format32bppArgb);

textBitmapFilled = new Bitmap(300, 300, PixelFormat.Format32bppArgb);

// Render the metafile onto a bitmap.

using (Graphics metafileBitmapGraphics = Graphics.FromImage(metafileBitmap))

{

// Commenting out the next line makes no difference.

metafileBitmapGraphics.FillRectangle(Brushes.White, 0, 0, 300, 300);

metafileBitmapGraphics.DrawImage(metafile, new Point(0, 0));

}

// Render text onto a newly created bitmap.

using (Graphics textBitmapBlankGraphics = Graphics.FromImage(textBitmapBlank))

{

textBitmapBlankGraphics.DrawString("Text on bitmap (unfilled bitmap).",new Font("Arial", 14), Brushes.Black, new Point(0, 0));

}

// Render text onto a new bitmap, after filling it with white.

using (Graphics textBitmapFilledGraphics = Graphics.FromImage(textBitmapFilled))

{

textBitmapFilledGraphics.FillRectangle(Brushes.White, 0, 0, 300, 300);

textBitmapFilledGraphics.DrawString("Text on bitmap (filled bitmap)", new Font("Arial", 14), Brushes.Black, new Point(0, 0));

}

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

if (!loaded) return;

using (Graphics formGraphics = this.CreateGraphics())

{

// Draw the metafile directly onto the form.

formGraphics.Clip = new Region(new Rectangle(0, 0, 300, 300));

formGraphics.DrawImage(metafile, new Point(0, 0));

formGraphics.ResetClip();

// Draw the bitmaps onto the form.

formGraphics.DrawImage(metafileBitmap, new Point(350, 0));

formGraphics.DrawImage(textBitmapBlank, new Point(0, 350));

formGraphics.DrawImage(textBitmapFilled, new Point(350, 350));

}

}

Chris49  Wednesday, September 12, 2007 2:32 PM
Hard to try your code without the metafile you used. You are not setting the Graphics.TextRenderHint property.

Hans Passant.
nobugz  Tuesday, September 15, 2009 2:28 AM
Perhaps the Graphics object has a "screen resolution" scaling. See for code and explanation:
http://nicholas.piasecki.name/blog/2009/06/drawing-o-an-in-memory-metafile-in-c-sharp/

-Jason
drjasonharrison  Monday, September 14, 2009 7:09 PM
Hard to try your code without the metafile you used. You are not setting the Graphics.TextRenderHint property.

Hans Passant.
nobugz  Tuesday, September 15, 2009 2:28 AM

You can use google to search for other answers

Custom Search

More Threads

• ImageList
• Exception in Graphics.DrawPath in special circumstances.
• Can i access text box in threading ?
• Binding a datasource to combobox
• reaching cell position
• Data Updates Fail With Tab Control
• Whitespace character removal when loading text file into a textBox
• Unique column in DatagridView
• Not really understanding CR
• Closing the dialog form