Re: Convert picture box content to jpeg??

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



You didn't miss much. The sample app is just that, a sample app that draws
some basic things using the API.

If you have a look at the mnuRedraw_Click method, you'll see that there are
a whole bunch of routines that have been commented out that illustrate doing
things with GDI+. Basically, you pick the function you want and see how that
particular function is implemented. The test app itself isn't really
expected to do anything spectacular.

In your case, to use this stuff, add a reference to the Gdiplus.tlb to you
app and include the GDIPlus API.bas in the project.

In the form load, call the InitGdip method to initialise the GDI library.
do what you need to in order to create the picture box and then Call the
method to export the image
In the form_unload method, call the ShutdownGdip method to close off the GDI
library.

The method to export the image is probably the most complicated. This one
will probably do for you, assuming the file name to create is passed as a
parameter and the picture box is called "picDial". I've taken out error
traps and a few debugging things as they're specific to my organisation.
This routine is part of an OCX in my case, if that matters.


Public Sub SaveDial(ByVal fileName As String)
'---------------------------------------------------------------------------------------
' Procedure: SaveDial
'
' Purpose: Draw the dial and save it to the specific file name.
'
' Author: Steve Barnett : 20 Aug 2008
'---------------------------------------------------------------------------------------
Dim Graphics As Long
Dim img As Long
Dim lngQuality As Long
Dim encoderCLSID As CLSID
Dim encoderParams As EncoderParameters
Dim stat As GpStatus

'*** We only save png files!
If LCase(Right$(fileName, 4)) <> ".png" Then
Err.Raise INVALID_FILENAME, SAVE_ERROR_SOURCE, "Dials must be saved as
PNG files"
End If

'*** Initialise the graphics class based on the picture box with the dial
in it
Call GdipCreateFromHDC(picDial.hDC, Graphics)

'*** Create the image "class" from the image in the picture box
PictureBox
Call GdipCreateBitmapFromHBITMAP(picDial.Image.Handle,
picDial.Image.hPal, img)

'*** Get the CLSID of the PNG encoder
encoderCLSID = GetEncoderClsid("image/png")

'*** Now save the file as a png. There are no parameters for png files
encoderParams.Count = 0
stat = GdipSaveImageToFile(img, fileName, encoderCLSID, encoderParams)

'*** Cleanup by deleting the image and releasing the graphics handle
Call GdipDisposeImage(img)
Call GdipDeleteGraphics(Graphics)

If stat <> Ok Then
Err.Raise FILE_SAVE_ERROR, SAVE_ERROR_SOURCE, "File save error. Error
status is: " & stat
End If
End Function

The GDI+ library is included with Windows from W2K sp4 onwards, so I'm not
distributing it with my install, so the size of the library is irrelevant to
my install. The benefits of simplicity and quality outweigh the size of the
library for my purposes. You will have to make that decision for yourself
though.

I should also say that I switched to GDI+ for a specific application where I
needed to draw smooth curves (using anti-aliasing functions), something I
couldn't do with any native VB functions. I also faced the same problem as
you in that I then wanted to save the results to a jpg, which I couldn't do
with a picture box. The bmp files I could get were way too large for web
usage. Hence the use of GDI+ following the advice I received through these
news groups.

In the end, I went for png files because gif files came out poor when my
picture had a lot of colours and jpg files required setting additional
options that I didn't want to have to offer to my users (compression and
quality settings).

Steve



"Randy Gardner" <RandyGardner@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:AF68828D-D29B-4FDC-BFD5-91658E98E9FF@xxxxxxxxxxxxxxxx
Steve:

I'm certianly not the sharpest tool in the wood shed, however, I
downloaded
from the website and ran the .vbp, nothing happens?

I see grapes in 2 picture boxes but redraw does nothing?
I tried changing parameters, color, pixels, etc.

Did I miss somethng?
--
Thank you,

Randy


"Steve Barnett" wrote:

You might want to go have a look at:

http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=43004&lngWId=1

This describes the GDI+ functions and provides a sample app that does
most
things you'll ever want to with GDI+. I adapted this code to save a
picture
box to a png file, precisely for use on web pages. The relevant bit of
code
comes down to:

'*** Initialise the graphics class based on the picture box with the
dial
in it
Call GdipCreateFromHDC(picDial.hDC, Graphics) ' Initialize the
graphics
class - required for all drawing

'*** Create the image "class" from the image in the picture box
PictureBox
Call GdipCreateBitmapFromHBITMAP(picDial.Image.Handle,
picDial.Image.hPal, img)

'*** Get the CLSID of the PNG encoder
encoderCLSID = GetEncoderClsid("image/png")

'*** Now save the file as a png. There are no parameters for png files
encoderParams.Count = 0
stat = GdipSaveImageToFile(img, fileName, encoderCLSID, encoderParams)

There are a few initialisation jobs to do as well as this, but they're
all
covered in the sample files. I have to say, the results I obtained were
excellent (I also used GDI+ to write to the picture box, so got
anti-alias
support).

You also have options for gif and jpeg files, if you prefer that format.

Steve


"Randy Gardner" <RandyGardner@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:B3086140-ADB7-4D65-B1E9-C3CCF4B11E83@xxxxxxxxxxxxxxxx
I want to take data (text & graphics) from a picture box and convert it
to
a
jpeg for display on a website. HOW?

Is there a size issue, picture box and website display window?


--
Thank you,

Randy





.



Relevant Pages

  • Re: creating multiple regions from an image based on colors
    ... Find great Windows Forms articles in Windows Forms Tips and Tricks ... Answer those GDI+ questions with the GDI+ FAQ ... and a picture box on the form and that bitmap as ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Drawing Multiple Polygons
    ... Answer those GDI+ questions with the GDI+ FAQ ... >I have a program that displays a map in a picture box. ... > draw polygons on top of the map. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Merging two or more pictures into one?
    ... It should be used in a vrml world, so we need .png files for transparancy. ... trouser ... as far as i know these new made picture will be used as avatar in a 3d vrml world. ...
    (comp.lang.java.programmer)
  • Re: Colorize 24-bit BMP Graphic?
    ... you're using the picture box control's size as opposed to the size of the Bitmap it contains when filling ... Failure to do so can cause your application to leak GDI ... Also your cleanup is being done in the wrong order; you're deleting the DC (which still has your DIBSection selected ...
    (microsoft.public.vb.winapi.graphics)
  • Re: Convert picture box content to jpeg??
    ... This describes the GDI+ functions and provides a sample app that does most ... I adapted this code to save a picture ... box to a png file, precisely for use on web pages. ...
    (microsoft.public.vb.general.discussion)