Solved

draw string in a bitmap

Posted on 2013-05-12
7
579 Views
Last Modified: 2013-05-14
hi, i have this code:

    objBmpImage = New Bitmap(objBmpImage, New Size(960, 832))
    objGraphics = Graphics.FromImage(objBmpImage)
    objGraphics.SmoothingMode = SmoothingMode.AntiAlias
    objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias
    objGraphics.DrawString(vtext, New Font("Arial", 35, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel), Brushes.White, 30, 25)


how can i draw this string inside my working area of the bitmap, so if the string is small, then the size will be bigger to fill the space, if the string is bigger then the size  it will be small. and how to justify the text.
0
Comment
Question by:rafaelrgl
  • 4
  • 2
7 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39159837
Try something like this:
        Dim margin As Integer = 30
        objBmpImage = New Bitmap(objBmpImage, New Size(960, 832))
        Using objGraphics As Graphics = Graphics.FromImage(objBmpImage)
            objGraphics.SmoothingMode = SmoothingMode.AntiAlias
            objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias

            Dim fSize As Integer = 7
            Dim F As Font = Nothing
            Do
                fSize = fSize + 1
                If Not IsNothing(F) Then
                    F.Dispose()
                End If
                F = New Font("Arial", fSize, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel)
            Loop While objGraphics.MeasureString(vtext, F).Width < objBmpImage.Width - (2 * margin)
            objGraphics.DrawString(vtext, F, Brushes.White, margin, 25)
            F.Dispose()
        End Using

Open in new window


It simply increases the size of the font until it no longer fits in the desired space.
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 39159972
hi idle_mind,

i´m sorry but i put the wrong line for the string, the right one is this one:

objGraphics.DrawString(vtext, New Font("Arial", 80, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel), Brushes.White, System.Drawing.RectangleF.FromLTRB(10, 120, 950, 810))

because the one i put before it~s just for one line, and i want to fit not only on the line but the text goes all the way to the end of the image, so i have an rectangleF, i need to scale to fit on this rectangle.

any help
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39159979
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:rafaelrgl
ID: 39159995
emoreau,

this is not what i want, because i am doing inside an rectangleF, and not just one line so the thext goes inside a box, and i want to scale the font to fit this box, so, if i have long text, the font will be set smaller to fit the area i am printing the text, if the text is short the font will be bigger so it will fit this area.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39160002
Is the text supposed to wrap?...or does it have distinct lines that must stay on those lines and you want to maximize how those lines fit in the box?
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 39160005
it's suppose to wrap
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 39160007
and be justify
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 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