Solved

Export a label to an image

Posted on 2006-11-28
4
183 Views
Last Modified: 2008-02-01
Hi All,

Is there any way to save what is in a label i.e. the text, to an image? I have a barcode font and would like to save the text as an image. Is there a way to do this?

Thanks
0
Comment
Question by:nzfire
  • 2
4 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
ID: 18027028
Hi nzfire,

You can build an image dynamically with the appropriate content:

Imports System.Drawing
Imports System.drawing.imaging
Imports System.drawing.drawing2d

 and then in your page_load event do:

        Dim strBarcode As String = "1234567890" 'The text to encode as the barcode

        'Get a dummy bitmap so we can work out how big the real one needs to be
        Dim b As New System.drawing.Bitmap(100, 100, PixelFormat.Format16bppRgb555)
        Dim salign As New System.drawing.StringFormat
        Dim g As Graphics = Graphics.FromImage(b)
        'determine the height and width of the barcode
        Dim w As Single = g.MeasureString(strBarcode.ToString, New Font("Free 3 Of 9 Extended", 20, FontStyle.Regular, GraphicsUnit.Point)).Width
        Dim h As Single = g.MeasureString(strBarcode.ToString, New Font("Free 3 Of 9 Extended", 20, FontStyle.Regular, GraphicsUnit.Point)).Height
        'get rid of the dummy bitmap and create a new one of the right size (add a bit to be sure if you want).
        b = Nothing
        b = New System.drawing.Bitmap(w * 1.1, h * 1.1, PixelFormat.Format16bppRgb555)
        g = Graphics.FromImage(b)
        g.SmoothingMode = SmoothingMode.AntiAlias ' antialias objects
        g.Clear(Color.White)  ' blank the image
        'Now write the barcode to the bitmap
        g.DrawString(strBarcode.ToString, New Font("Free 3 Of 9 Extended", 20, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, 0, 0)
        'Set the image to a picturebox object - you can do whatever else you need to with it here as well, such as save it to a file or whatever.
        PictureBox1.Image = b
        g = Nothing
        b = Nothing

This is an example with a freeware barcode font. You should substitute your own font above.

Tim Cottee
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 18027029
nzfire,

Ignore the page_load event part, that was from an asp.net version of this, you just need to put that code in a procedure somewhere that you can call it.

Tim
0
 
LVL 10

Expert Comment

by:gangwisch
ID: 18027123
cant you do label1.drawtobitmap?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18028805
I believe DrawToBitmap() is only available in VB.Net 2005...

You could also take a screenshot of the Label:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21627632.html
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

19 Experts available now in Live!

Get 1:1 Help Now