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));
elseobjBmpImage = 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