Results 1 to 5 of 5

Thread: (2005) How can I draw a string on a picture file?

  1. #1

    Thread Starter
    Addicted Member javad2000's Avatar
    Join Date
    Dec 2006
    Posts
    238

    (2005) How can I draw a string on a picture file?

    Hello

    How is it possible to write a string on a picture file? Ex:

    I want to write:
    "This is a test"

    on:
    C:\Test.bmp

    and save it as:
    C:\Test1.bmp

    Thank you

  2. #2
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: (2005) How can I draw a string on a picture file?


  3. #3

    Thread Starter
    Addicted Member javad2000's Avatar
    Join Date
    Dec 2006
    Posts
    238

    Re: (2005) How can I draw a string on a picture file?

    Quote Originally Posted by Ken B
    Thank you very much Ken B
    The solution was quite ez and user-friendly.

    Here's the code
    Code:
    Sub DrawOnPicture(ByVal StrInputFilePath As String, ByVal StrOutputFilePath As String, ByVal StrWaterMark As String)
            'Construct a Bitmap object from a jpg's filename:
            Dim bmp As Bitmap = New Bitmap(StrInputFilePath)
            'Obtain a Graphics object from & for that Bitmap:
            Dim canvas As Graphics = Graphics.FromImage(bmp)
            'Draw the watermark string onto the Bitmap:
            canvas.DrawString(strWatermark, _
            New Font("Verdana", 14, FontStyle.Bold), _
            New SolidBrush(Color.Beige), 0, 0)
            'Save the watermarked bitmap to a new file:
            bmp.Save(StrOutputFilePath)
        End Sub
    I have only 1 more problem.
    How can I know the Height and Width of the "String" and also the Height and width of the "Picture" (in pixels)?

    ('cause I want to draw the string on the right side of the pic)

    Thank you

  4. #4
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: (2005) How can I draw a string on a picture file?

    I started to work on this 6 months ago and got side tracked. Looking at my code, I found that you will have a problem writing text on a Format8bppIndexed image.

    I have attached an image in the above format. Try your existing code on it and see if it works.

    The following code will tell you if an image is a Format8bppIndexed image.

    Code:
      Public Structure imageInformation
    
        Dim Caption As String
        Dim FileExist As Boolean
        Dim FileName As String
        Dim Height As Single
        Dim HorizontalResolution As Single
        Dim ImageType As String
        Dim Length As Double
        Dim Name As String
        Dim Palette As Single
        Dim PixelFormat As String
        Dim Postion As Int16
        Dim PrimaryImage As Boolean
        Dim ResizeHeight As Single
        Dim ResizePercentage As Single
        Dim ResizeWidth As Single
        Dim SaveAsFileName As String
        Dim UpdatedAt As String
        Dim ValidImage As Boolean
        Dim VerticalResolution As Single
        Dim Width As Single
    
      End Structure
    
      Public Sub getImageFileInfo(ByVal fileName As String, ByRef tmpImage As imageInformation)
    
        If Not System.IO.File.Exists(fileName) Then
          tmpImage.ValidImage = False
          Exit Sub
        End If
    
        Try
          Dim bit As New Bitmap(fileName)
          tmpImage.FileExist = True
          tmpImage.FileName = fileName
          tmpImage.Height = bit.Height
          tmpImage.HorizontalResolution = bit.HorizontalResolution
          tmpImage.Length = FileLen(fileName)
          tmpImage.PixelFormat = bit.PixelFormat.ToString
          tmpImage.ImageType = bit.GetType.ToString
          tmpImage.ValidImage = True
          tmpImage.VerticalResolution = bit.VerticalResolution
          tmpImage.Width = bit.Width
          bit.Dispose()
        Catch ex As Exception
          tmpImage.FileName = fileName
          tmpImage.ValidImage = False
          Call displayMessage("Exception in getImageFileInfo." & Environment.NewLine & Environment.NewLine & "Error: " & ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    
      End Sub
    If it is, all you have to do is recreate it as a 24bit image using the following:

    Code:
        Dim orginalImage As New Bitmap(ImageFileName)
        Try
          If System.IO.File.Exists(NewImageFileName) Then
            System.IO.File.Delete(NewImageFileName)
          End If
          orginalImage.SetResolution(NewResolution, NewResolution)
          orginalImage.Save(NewImageFileName, System.Drawing.Imaging.ImageFormat.Jpeg)
          orginalImage.Dispose()
    
        Catch exIO As System.IO.IOException
          MessageBox.Show("IOException in reCreateImage." & Environment.NewLine & Environment.NewLine & exIO.Message, "vbPDF Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
          Return False
    
        Catch ex As Exception
          MessageBox.Show("Exception in reCreateImage." & Environment.NewLine & Environment.NewLine & ex.Message, "vbPDF Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
          Return False
    
        Finally
          orginalImage.Dispose()
    
        End Try
    The above code is from two different appliactions, but they should work.

    I can't help you with your size question at the momment, but will look into it later today.

    Ken
    Attached Images Attached Images  

  5. #5

    Thread Starter
    Addicted Member javad2000's Avatar
    Join Date
    Dec 2006
    Posts
    238

    Re: (2005) How can I draw a string on a picture file?

    You are right.
    It said that the picture has an indexed pixel and cannot be converted to a graphics object.

    Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width