Solved

Export a label to an image

Posted on 2006-11-28
4
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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