locked
How to make a generated jpg image to align center? RRS feed

  • Question

  • User-739075062 posted

    I have a programmed image with text strings written into it. When it displays, it's to the left of a page. How do I make it align to the center? My code is like this

    Dim Image1 As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("images/workplace_violence_certificate.jpg"))
            Dim oGraphic As Graphics = Graphics.FromImage(Image1)
            'oGraphic.DrawImage(Image1, new Point(1000,0))
            Dim oColor As System.Drawing.Color = Color.Black
            Dim sText As String = "student name"
            Dim dateText As String = "ON 1/1/2010"
            Dim oBrush As New SolidBrush(Color.White) 'background
            Dim oBrushWrite As New SolidBrush(Color.Black) 'text
            oGraphic.FillRectangle(oBrush, 668, 600, 468, 60)
            Dim oFont As New Font("Arial", 30)
            Dim oPoint As New PointF(900F, 600F)
            oGraphic.DrawString(sText, oFont, oBrushWrite, oPoint)
            oGraphic.DrawString(dateText, oFont, oBrushWrite, New PointF(930F,1000F))
            
           Image1.Save(Server.MapPath("gen_img.jpg"), ImageFormat.Jpeg)

    Thursday, July 29, 2010 3:30 PM

Answers

  • User1485408165 posted

    I see I made a mistake in my calculations for X and Y on the rectangle.  using oPoint makes more sense

     

    Code:

            Dim oPoint As New PointF(Image1.Width / 2 - drawingSize.Width / 2, Image1.Height / 2 - 50)
    
            oGraphic.FillRectangle(oBrush, oPoint.X, oPoint.Y, CInt(drawingSize.Width), CInt(drawingSize.Height))
            oGraphic.DrawString(sText, oFont, oBrushWrite, oPoint)
    
            drawingSize = oGraphic.MeasureString(dateText, oFont, Image1.Width, Nothing)
            oBrush = New SolidBrush(Color.Wheat) 'background
    
            oPoint = New PointF(Image1.Width / 2 - drawingSize.Width / 2, Image1.Height / 2 + 50)
            oGraphic.FillRectangle(oBrush, oPoint.X, oPoint.Y, CInt(drawingSize.Width), CInt(drawingSize.Height))
            oGraphic.DrawString(dateText, oFont, oBrushWrite, oPoint)
    
            Image1.Save(Server.MapPath("gen_img.gif"), ImageFormat.Jpeg)


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 29, 2010 5:57 PM

All replies

  • User1485408165 posted

    do you want the image you've created dynamically to be centered on the page or do you want the text that you draw on the image to be centered on the Image?

     

    Thursday, July 29, 2010 5:17 PM
  • User1485408165 posted

    Soultion to either question I posed.

     used <div style="text-align: center;"></div> to center align the image on the page.

     

    Use this code to align the text inside the picture.

    Dim Image1 As Image = Image.FromFile(Server.MapPath("images/known.jpg"))

    Dim oGraphic As Graphics = Graphics.FromImage(Image1)

    Dim oColor As Color = Color.Black

    Dim sText As String = "Joe"

    Dim dateText As String = "ON 7/31/2010"

    Dim oBrush As New SolidBrush(Color.White) 'background

    Dim oBrushWrite As New SolidBrush(Color.Black) 'text

    Dim oFont As New Font("Arial", 45)

    Dim drawingSize = oGraphic.MeasureString(sText, oFont, Image1.Width,Nothing)

    Dim oPoint As New PointF(Image1.Width / 2 - drawingSize.Width / 2, Image1.Height / 2 - 50)

    oGraphic.FillRectangle(oBrush, CInt(Image1.Width / 2 - drawingSize.Width / 2), CInt(Image1.Height / 2 - drawingSize.Height), CInt(drawingSize.Width), CInt(drawingSize.Height))

    oGraphic.DrawString(sText, oFont, oBrushWrite, oPoint)

    drawingSize = oGraphic.MeasureString(dateText, oFont, Image1.Width, Nothing)

    oPoint = New PointF(Image1.Width / 2 - drawingSize.Width / 2, Image1.Height / 2 + 50)

    oGraphic.DrawString(dateText, oFont, oBrushWrite, oPoint)

    Image1.Save(Server.MapPath("gen_img.gif"), ImageFormat.Jpeg)

     

    Thursday, July 29, 2010 5:49 PM
  • User1485408165 posted

    I see I made a mistake in my calculations for X and Y on the rectangle.  using oPoint makes more sense

     

    Code:

            Dim oPoint As New PointF(Image1.Width / 2 - drawingSize.Width / 2, Image1.Height / 2 - 50)
    
            oGraphic.FillRectangle(oBrush, oPoint.X, oPoint.Y, CInt(drawingSize.Width), CInt(drawingSize.Height))
            oGraphic.DrawString(sText, oFont, oBrushWrite, oPoint)
    
            drawingSize = oGraphic.MeasureString(dateText, oFont, Image1.Width, Nothing)
            oBrush = New SolidBrush(Color.Wheat) 'background
    
            oPoint = New PointF(Image1.Width / 2 - drawingSize.Width / 2, Image1.Height / 2 + 50)
            oGraphic.FillRectangle(oBrush, oPoint.X, oPoint.Y, CInt(drawingSize.Width), CInt(drawingSize.Height))
            oGraphic.DrawString(dateText, oFont, oBrushWrite, oPoint)
    
            Image1.Save(Server.MapPath("gen_img.gif"), ImageFormat.Jpeg)


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 29, 2010 5:57 PM
  • User1211441112 posted

    This will help you

    http://forums.asp.net/t/1554715.aspx

    Wednesday, August 4, 2010 2:48 AM