Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 8th, 2012, 03:17 AM
Authorized User
Points: 39, Level: 1
Points: 39, Level: 1 Points: 39, Level: 1 Points: 39, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems with drawing in .NET

Hi,

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.

Code:
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));
else
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,

-Ferdy
Reply With Quote
  #2 (permalink)  
Old February 10th, 2012, 04:21 AM
Authorized User
Points: 39, Level: 1
Points: 39, Level: 1 Points: 39, Level: 1 Points: 39, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If someone could shed some light on this it'd be much appreciated :(
Reply With Quote
  #3 (permalink)  
Old February 16th, 2012, 03:22 AM
Authorized User
Points: 39, Level: 1
Points: 39, Level: 1 Points: 39, Level: 1 Points: 39, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

-Ferdy
Reply With Quote
  #4 (permalink)  
Old February 18th, 2012, 02:54 AM
Friend of Wrox
Points: 742, Level: 10
Points: 742, Level: 10 Points: 742, Level: 10 Points: 742, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2009
Location: Tehran, Iran
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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 06:22 PM



All times are GMT -4. The time now is 02:17 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.