locked
GDI+ and font alignment RRS feed

  • Question

  • User-81382370 posted
    Hi I can use this code to write text to an exsisting image... myGraphic.DrawString("hello" & vbCrLf & "this is the text", New Font("Helvetica", 10, FontStyle.Bold), New SolidBrush(Color.Black), New PointF(40, 150)) But is there a way to center the text? Also, can I only use fonts that has no spaces in the font name. It seems that I'm only able to do that or else I'm doing something wrong... Regards M
    Sunday, August 1, 2004 2:01 AM

All replies

  • User-46888941 posted
    - But is there a way to center the text? Check the height and length and divide them by 2. myGraphic.DrawString("hello" & vbCrLf & "this is the text", New Font("Helvetica", 10, FontStyle.Bold), New SolidBrush(Color.Black), New PointF((myGraphic.Height / 2), (myGraphic.Height / 2)) Its just pseudo code.. Don´t know if it works ;)
    Tuesday, August 10, 2004 1:55 PM
  • User2064082449 posted
    Use this: StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center;
    Wednesday, August 11, 2004 8:20 PM
  • User-1372641848 posted
    You can use System.Drawing.StringFormat to format verticle and horizontal alignment of text, try following sample code run sample <%@ Page Language="C#"%> <%@ Import Namespace="System.Drawing" %> <script runat="server"> void Page_Load(Object sender,EventArgs e){ Bitmap bmp = new Bitmap(300,50); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); // Center Align Rectangle r = new Rectangle(0,0,299,49); StringFormat f = new StringFormat(); // Change this to change horizontal alignment. f.Alignment = System.Drawing.StringAlignment.Center; // Change this to chenge verticle alignment. f.LineAlignment = System.Drawing.StringAlignment.Center; g.DrawRectangle(Pens.Black,r); g.DrawString("CenterAlign",new Font("Verdana",12),Brushes.Black,r,f); bmp.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif); } </script>
    Wednesday, August 11, 2004 9:36 PM