Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

draw string in a bitmap

Posted on 2013-05-12
7
Medium Priority
?
585 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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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 86

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

916 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