Solved

draw string in a bitmap

Posted on 2013-05-12
7
577 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 69

Expert Comment

by:Éric Moreau
ID: 39159979
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now