Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create proportional font space in  System.Drawing.Graphics

Posted on 2008-06-22
2
Medium Priority
?
1,097 Views
Last Modified: 2010-04-21
Hi

I am using C#, in a asp.net webform

I am trying to write a tiff file from text using using System.Drawing and System.Drawing.Imaging. The problem I am having is that the font is not propotional. So when I do a line break in the text,  "III 111" does not line up with "ABC 976" in the row above it. How can I get the data to line up proportionally?
string FontName = "Verdana";
        System.Drawing.Color FontColor = Color.Black;
        System.Drawing.Color BackColor = Color.White; 
        int FontSize = 14; 
        int Width = 200; 
        //fontsize times 1.5 is just high enough 
        //to encase the text without spacer above or below.         
        float h = (FontSize * 1.5f);
        int Height = 200; // int.Parse(h.ToString());         
        //file to save as 
        //fore color 
        System.Drawing.SolidBrush objBrushForeColor = new SolidBrush(FontColor);         
        //back color 
        System.Drawing.SolidBrush objBrushBackColor = new SolidBrush(BackColor);         
        //the point to start the text. I chose horizontal value of zero 
        //vertical starts at 2 pixels down. 
        System.Drawing.Point objPoint = new Point(0, 2); 
        //font object 
        System.Drawing.Font objFont = new Font(FontName, FontSize);
        //Drawing.Font(family, 32, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
 
        //bitmap object 
        System.Drawing.Bitmap objBitmap = new Bitmap(Height, Width); 
        //graphics object 
        System.Drawing.Graphics objGraphics = System.Drawing.Graphics.FromImage(objBitmap); 
        //the following line is not needed, but is shown 
        //in the vb example.. dont know why. 
        //Color objColor; 
        //draw a white rectangle 
        objGraphics.FillRectangle(objBrushBackColor, 0, 0, Width, Height);           // (FontSize*1.5f)); 
        //draw the text 
        objGraphics.DrawString("ABC 123\r\nCDE 345\r\nDEF 456\r\nIII 111", objFont, objBrushForeColor,objPoint,
        //save the bitmap. 
        objBitmap.Save(@"C:\CreateThumbnailImage\file.tiff", ImageFormat.Tiff);
        Response.Write("done");

Open in new window

0
Comment
Question by:Charles Baldo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
Pacman earned 2000 total points
ID: 21843912
The font you are using (Verdana) is a proportional font.
Take a fixed font (courier or courier new).

string FontName = "Courier New";

Open in new window

0
 

Author Closing Comment

by:Charles Baldo
ID: 31469628
Perfect, thanks a ton
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question