Wrox Programmer Forums
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 8th, 2012, 04:17 AM
Authorized User
Join Date: Nov 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems with drawing in .NET


For a project we have been drawing text entered by the user and pass the resulting image to a PDF renderer that enters the image into the PDF that's about to be rendered.

Bitmap objBmpImage = new Bitmap(1, 1);
int intWidth = 0;
int intHeight = 0;

// Create a graphics object to measure the text's width and height.
Graphics objGraphics = Graphics.FromImage(objBmpImage);

// This is where the bitmap size is determined.
StringFormat format1 = new StringFormat(StringFormat.GenericTypographic);
format1.FormatFlags = StringFormatFlags.NoClip;
intWidth = (int)objGraphics.MeasureString(sImageText, objFont, 100000, new StringFormat(format1)).Width;
intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;

// Create the bmpImage again with the correct size for the text and font.
if (intWidth == 0 || intHeight == 0)
objBmpImage = new Bitmap(objBmpImage, new Size(1, 1));
objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
//Remake the graphics object with the new bitmap objGraphics = Graphics.FromImage(objBmpImage); // Determine rendering settings //objGraphics.Clear(Color.FromArgb(150, Color.White));//<- use this if you need to check the rendered textbox! objGraphics.Clear(Color.Transparent);//<- use this for live version! objGraphics.SmoothingMode = SmoothingMode.AntiAlias; objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias; objGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; objGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality; string[] colorVals = functions.htmlHexToRGB(FontColor).Split(' '); int red = int.Parse(colorVals[0]); int green = int.Parse(colorVals[1]); int blue = int.Parse(colorVals[2]); //Draw the string if (intWidth == 0 || intHeight == 0)
objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.Transparent), 0, 0);
else {
StringFormat sFormat = new StringFormat(StringFormat.GenericTypographic); sFormat.Alignment = StringAlignment.Near; sFormat.FormatFlags = StringFormatFlags.NoClip; objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(red, green, blue)), 0, 0, sFormat);
} //Clear the graphics object objGraphics.Flush(); //Get the textbox' original width&height int textBoxHeight = objBmpImage.Height; int textBoxWidth = objBmpImage.Width; //convert radians to degrees float r = ((float)Rotatie * -1);//0; //Call the rotate function. (Point X/Y need to be the center of the object!) objBmpImage = RotateImage(factor, objBmpImage, new PointF((float)objBmpImage.Width / 2, (float)objBmpImage.Height / 2), r); //Get the new height. Since it's a square, width&height are the same int boundingBoxHeight = objBmpImage.Height; //Make a Bitmap with the size of the final PDF Bitmap bmpPage = new Bitmap(pdf_width, pdf_height, PixelFormat.Format32bppArgb); //Make new graphics object Graphics gfxPage = Graphics.FromImage(bmpPage); //define rendering option gfxPage.SmoothingMode = SmoothingMode.AntiAlias; gfxPage.InterpolationMode = InterpolationMode.HighQualityBicubic; gfxPage.PixelOffsetMode = PixelOffsetMode.HighQuality; gfxPage.Clear(Color.Transparent); /* * This is for positioning the block correctly! * Since the square is larger than the real thing, you need to compensate to get the text placed correctly */ int xText = x - ((boundingBoxHeight - textBoxWidth) / 2); int yText = y - ((boundingBoxHeight / 2) - (textBoxHeight / 2)); //Draw the final image gfxPage.DrawImage(objBmpImage, new Rectangle(xText, yText, WIDTH?, HEIGHT?), 0, 0, pdf_width, pdf_height, GraphicsUnit.Pixel); return (bmpPage);
The part that is giving me issues is the second last line in red. The rotate function for my text works correctly and returns a Bitmap object. It's a nice square, with the text centered inside it and rotated the way it needs to be.

This image containing the rotated text is the objBmpImage object. I put that in the DrawImage function of the gfxPage object to render it inside the bmpPage object.

So far so good. The problem however, is the rectangle I need to define in the the DrawImage function. From what I understand, the X and Y parameters are the X/Y position of the upper left corner of the Rectangle object WITHIN the final bitmap.

So, if I were to enter 50, 100 I would expect the rectangle to start at 50 pixels from the left and 100 from the top of the bmpPage Bitmap.

The width and height parameters, to my understanding, should be the size of the bitmap I want to place inside the new bitmap. In my case, they should be the width and height of objBmpImage.

.NET should then put in the objBmpImage Bitmap and position it correctly. This means that if the width, height parameters inserted in the DrawImage (for me they are the variables pdf_width and pdf_height) are smaller then the size of my rectangle, a part of the rectangle should simply overflow the boundary and get cut-off in the final image.

This however, is not what is happening. If I put the dimensions of the objBmpImage as WIDTH? and HEIGHT?, it distorts my text. If I put the final PDF dimensions in there, it can cut off the objBmpImage in a weird and unexpected manner.

What do I do here? What am I misunderstanding about the DrawImage function?

Kind regards,

Old February 10th, 2012, 05:21 AM
Authorized User
Join Date: Nov 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts

If someone could shed some light on this it'd be much appreciated :(
Old February 16th, 2012, 04:22 AM
Authorized User
Join Date: Nov 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts

I hate triple posting and bumping, but I could really use some help here.

Old February 18th, 2012, 03:54 AM
Friend of Wrox
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Smile please wait ... !

Hi Fred

don't worry!, I'll test it as soon as possible. now I'm at my work office, at night I'll test it.
happy every time, happy every where

Reza Baiat

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrox Video: Drawing with .NET jminatel ASP.NET 2.0 Professional 1 April 19th, 2009 05:44 AM
drawing fergo Access VBA 0 June 6th, 2005 12:35 PM
drawing fergo BOOK: Access 2003 VBA Programmer's Reference 1 June 5th, 2005 09:32 AM
Drawing in C# stripytiger VS.NET 2002/2003 4 August 25th, 2004 10:01 AM
drawing with VBA cutovoi Excel VBA 1 February 19th, 2004 07:22 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.