Solved

Export a label to an image

Posted on 2006-11-28
4
185 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

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