PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
how to save controls as jpg image-VBForums
Results 1 to 2 of 2

Thread: how to save controls as jpg image

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2007
    Posts
    44

    how to save controls as jpg image

    Hi
    I need to save the content of a control like RichTextBox or ListBox as a jpg image.

    how can it be done ?

    thanks

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,741

    Re: how to save controls as jpg image

    Well, first up you would not want to simply copy what the user sees on screen because both those controls could have content that is scrolled out of view. What you should do is create a Bitmap object, draw the contents of the control onto that Image and then save it as a JPG, e.g.
    vb.net Code:
    1. Const MAX_WIDTH As Integer = 500
    2. Dim h As Integer = Me.ListBox1.Items.Count * Me.ListBox1.ItemHeight
    3. Dim w As Integer = 0
    4. Dim temp As Integer
    5.  
    6. 'Determine the required width of the image up to a maximum of 500 pixels.
    7. Using g As Graphics = Me.CreateGraphics()
    8.     For Each item As String In Me.ListBox1.Items
    9.         temp = CInt(g.MeasureString(item, Me.ListBox1.Font, MAX_WIDTH).Width)
    10.  
    11.         If temp > w Then
    12.             w = temp
    13.         End If
    14.     Next item
    15. End Using
    16.  
    17. Dim b As New Bitmap(w, h)
    18. Dim p As Point = Point.Empty
    19. Dim r As New Rectangle(0, 0, w, h)
    20. Dim f As New StringFormat
    21.  
    22. f.Alignment = StringAlignment.Center
    23.  
    24. 'Draw each item onto the image.
    25. Using g As Graphics = Graphics.FromImage(b)
    26.     For Each item As String In Me.ListBox1.Items
    27.         g.DrawString(item, Me.ListBox1.Font, Brushes.Black, r, f)
    28.     Next item
    29. End Using
    30.  
    31. b.Save("file path here", Imaging.ImageFormat.Jpeg)
    That's untested but it should be about right. Note that this is pretty much how printing is done in VB.NET, except that the Graphiocs object is provided for you in the PrintPage event handler of a PrintDocument.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width