[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

How to save a picturebox's image to a .PNG file? (VB6

Nando

8/1/2010 7:18:00 PM

Hi all! I have a little program (written in VB6) that saves the image in
a picturebox to a BMP file. The problem is that BMP files are several
megabytes in size and do not support DPI (dots-per-inch) information for
printing.

I have turned to the PNG format which provides lossless compression
(files are only few kilobytes) and includes DPI information.

But how do I save the image in a picturebox to a PNG file?

Thanks in advance!
37 Answers

Mike Williams

8/1/2010 8:40:00 PM

0

"Nando" <hightech@att.net.no.to.sp.am> wrote in message
news:uflaC5aMLHA.2100@TK2MSFTNGP06.phx.gbl...

> Hi all! I have a little program (written in VB6) that saves the
> image in a picturebox to a BMP file. The problem is that BMP
> files are several megabytes in size and do not support DPI
> (dots-per-inch) information for printing. I have turned to the
> PNG format which provides lossless compression (files are
> only few kilobytes) and includes DPI information.

The png format will give you those kind of lossless compressions (50:1 or
even more) on simple images, such as line drawings with filled sections and
graphs and other images with a limited number of colours and many small or
large areas of a solid colour, but the png lossless compression of a tpyical
photo will be much less, perhaps in the order of 2:1 or something of that
nature. So, if your images are of the kind I first mentioned then png is
definitely one way to go if you want high compression, but if they are
typical photos (and if compression is your main concern) then the lossy but
still quite good compression of a jpg might be more suitable (although the
fact that png can give you lossless compression on photos might be exactly
what you want if compression is not your main concern). As far as DPI
information in an image file is concerned, that's a moot point that I won't
go into here because I sometimes get into lots of trouble from various
people for writing too much waffle :-)

> But how do I save the image in a picturebox to a PNG file?

I reckon GDI+ is probably your best bet. Have a look at:

http://www.vbaccelerator.com/home/vb/code/vbmedia/using_gdi_plus/Reading_and_Writing_JPG__PNG__TIF_and_GIF_Files/a...

Mike




MikeD

8/1/2010 8:56:00 PM

0



"Nando" <hightech@att.net.no.to.sp.am> wrote in message
news:uflaC5aMLHA.2100@TK2MSFTNGP06.phx.gbl...
> Hi all! I have a little program (written in VB6) that saves the image in a
> picturebox to a BMP file. The problem is that BMP files are several
> megabytes in size and do not support DPI (dots-per-inch) information for
> printing.
>
> I have turned to the PNG format which provides lossless compression (files
> are only few kilobytes) and includes DPI information.
>
> But how do I save the image in a picturebox to a PNG file?


VB6 does not natively support the PNG format. Look for an OCX or DLL that
does (sorry, I personally don't know of any or I'd suggest one). Or, I
suppose you could search the Internet for the PNG format's file spec and
write the file yourself. Finding an OCX/DLL would probably be a lot easier,
and I'd be willing to bet there are free ones out there that shouldn't be
too hard to find.

--
Mike



tmoran4511

8/2/2010 3:18:00 AM

0

Nando...

There are plenty of free resources out there for saving an image in
the PNG format.

One is the Free Image open source DLL. It even has VB6 examples.
It's open source and available at:

http://freeimage.sourceforge.net/sourc...

Even Microsoft provides that ability with the Windows Image
Acquisition Library (WIA) found here:

http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=a332a77a-01b8-4de6-91c2-b7...

Mike Williams also gave you a good resource at vbAcclerator.

And, btw, Mike.... one man's waffle is another man's treasure. I
usually learn something with your "waffles". Don't stop now!

Tom

Horst Heinrich Dittgens

8/2/2010 2:09:00 PM

0

> And, btw, Mike.... one man's waffle is another man's treasure. I
> usually learn something with your "waffles". Don't stop now!

what I would like to underwrite, too!

se

8/2/2010 2:32:00 PM

0


"Shotgun Thom" <tmoran4511@gmail.com> skrev i meddelelsen
news:ec98930c-1768-4419-8b51-2ab6a77baaa7@t5g2000prd.googlegroups.com...
> Nando...
>
> There are plenty of free resources out there for saving an image in
> the PNG format.
>
> One is the Free Image open source DLL. It even has VB6 examples.
> It's open source and available at:
>
> http://freeimage.sourceforge.net/sourc...
>
> Even Microsoft provides that ability with the Windows Image
> Acquisition Library (WIA) found here:
>
> http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=a332a77a-01b8-4de6-91c2-b7...
>
> Mike Williams also gave you a good resource at vbAcclerator.
>
> And, btw, Mike.... one man's waffle is another man's treasure. I
> usually learn something with your "waffles". Don't stop now!
>
> Tom
>

It constitutes a long chain to trace back:

* From: "senn" <senn@homeplace&.fix>
* Date: Sun, 18 May 2008 19:34:26 +0200

"Mike Williams" <mikea@xxxxxxxxxxxxxxxxx> skrev i en meddelelse
news:ucj7kpPuIHA.3968@xxxxxxxxxxxxxxxxxxxxxxx

Okay, Senn. I'll give it a whirl later. What do I need to download and
install in order to make it work? Can you post a link?

Mike


GDI+ TypeLib TLB
-Unicode Logo
Generator
http://www.planet-source-code.com/upload_PSC/ftp/GDI+_TypeL154059...

/senn

And Karls answer to this back then:

None of that nonsense. And, even if you're stuck using that other lib, you
can
still use FreeImage_GetFileType to tell you what sort of file you're dealing
with.
:-)
--
...NET: It's About Trust!

http://vfre...

/se

Nando

8/2/2010 8:09:00 PM

0

senn wrote:
>
> GDI+ TypeLib TLB
> -Unicode Logo
> Generator
> http://www.planet-source-code.com/upload_PSC/ftp/GDI+_TypeL154059...
>
> /senn
>
> And Karls answer to this back then:
>
> None of that nonsense. And, even if you're stuck using that other lib,
> you can
> still use FreeImage_GetFileType to tell you what sort of file you're
> dealing with.
> :-)

Hi guys! I must share that I spent a great deal of time yesterday and
today understanding and implementing the GDI+ TypeLib routines within my
app. I just couldn't resist the temptation of having more than one file
type to save to (I only needed .PNG but I got greedy). GDI+ seemed to
offer so many other file types. Unfortunately after so much work and
troubleshooting I found I can only generate 32-bit transparent PNG files
and never 24-bit ones (just the very ones I need, since the 24-bit PNGs
do not support transparency and I cannot have transparency :-( Oh
well... can somebody just confirm this? Thanks!

-Nando

P.S.: Seems like the parameter "EncoderColorDepth" can only be set for
..JPG and .TIF. Seems like the GDI+ PNG codec just ignore any parameter.

Nando

8/2/2010 8:24:00 PM

0

Shotgun Thom wrote:
>
> There are plenty of free resources out there for saving an image in
> the PNG format.
>
> One is the Free Image open source DLL. It even has VB6 examples.
> It's open source and available at:
>
> http://freeimage.sourceforge.net/sourc...

That is great Tom! I'll definetly look into that free image library. I
like the fact that you say they have VB6 examples! Awesome!

> Even Microsoft provides that ability with the Windows Image
> Acquisition Library (WIA) found here:
>
> http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=a332a77a-01b8-4de6-91c2-b7...

Interesting, I would like to try that (WIA Lib). Although I'm quite
scare now, since I spent too much time already with another Microsoft
library (GDI+) just to find out that it does not really do what I
wanted. I'll look into WIA Lib, but with reserves.

I'm not doing too much image manipulation in my app, just need to save a
bitmap file to a 24-bit PNG file. I also needed to read the bitmap and
create a new one setting DPI and resize it using basic
"Nearest-Neighbor" interpolation. I had all that figured out and set up
in my app using GDI+ (which until yesterday I never worked with). Now I
have to start from scratch. Hopefully the Free Image Library will be
more helpful.

-Nando

Nando

8/13/2010 3:40:00 AM

0

Shotgun Thom wrote:
> Hi Nando...
>
> An even easier solution is to add the code included in this post to
> your project in a Module:
>
> It allows you to save your Picture Box as a 24bit PNG, JPG or BMP.
>
> Once you have the picture displayed in your picture box you just call
> the Sub as follows:
>
> SaveImageAs "c:\mypicture.png", Picture1.hdc, Picture1.Width /
> Screen.TwipsPerPixelX, Picture1.Height / Screen.TwipsPerPixelY,
> CLng(100)
>
> Open a new module in your project. Copy the following code and paste
> into the module. Use the syntax above to call saving the image,
> replacing, obviously, the file name with your own and with whatever
> you named your Picture Box.
>
><Snipped>
>
> Thanks to Ed Wilk for this code I downloaded from somewhere but can't
> remember.
>
> Tom

Thanks a billion Tom!!! I'm now able to create 24-bit PNG files!!!

I'm multitasking a lot of stuff on my side, but one thing I'm still
experiment (just for curiosity) is why (for a specific BMP image sample)
I get different file sizes of 18KB and 23KB.

The sample image: A snapshot of the calculator of Windows XP as a BMP
file (446KB).

The PNG version of the file is 23KB (using the API code). I also get the
same result if I save the file manually as a PNG from MS Paint.

However, I get a smaller PNG version of 18KB saving the sample file as
PNG using Microsoft Photo Editor 3 (which shipped with Office XP or
2003). This was a smaller size without any loss in quality since I
checked by re-saving the PNG as a BMP and comparing with the sample, and
they matched bit by bit (SourceForge's WinMerge).

Just wondering how Photo Editor was able to produce a file 78% smaller
under the same PNG format. The quality specified using the API was set
to 100, so I'm quite intrigued.

-Nando

tmoran4511

8/13/2010 5:57:00 AM

0

First of all, the Quality Setting included in the code I posted is
only applied when saving a file as a JPEG. 75 is a reasonable setting
for quality of a JPEG image that's purpose is to be viewed on a
monitor. Good compression and reasonable quality. Get much lower
than that and you may begin to notice the image deterioration.

Regarding the PNG file size differences... it all has to do with
compression techniques. There are a bunch of tool/utilities
(PngCrush, PNGOUT, etc.) out there that do nothing but compress
existing PNG files. There's even an online site (www.punypng.com)
where you upload your PNG and it will reduce it for you and send it
back. Google PNG Compression Tools and you'll see the plethora of
choices.

The nice thing about all these compression tools is there, usually, is
no loss of image quality. The API code uses a pretty standard png
saving format. Other utilities/programs, obviously, have tweaked the
compression. Kinda like the difference between WinZip, GZip and 7Zip
compression. They are all Zip tools... just different compression.

Tom

Nando

8/13/2010 8:49:00 AM

0

Shotgun Thom wrote:
> Regarding the PNG file size differences... it all has to do with
> compression techniques. There are a bunch of tool/utilities
> (PngCrush, PNGOUT, etc.) out there that do nothing but compress
> existing PNG files. There's even an online site (www.punypng.com)
> where you upload your PNG and it will reduce it for you and send it
> back. Google PNG Compression Tools and you'll see the plethora of
> choices.
>
> The nice thing about all these compression tools is there, usually, is
> no loss of image quality. The API code uses a pretty standard png
> saving format. Other utilities/programs, obviously, have tweaked the
> compression. Kinda like the difference between WinZip, GZip and 7Zip
> compression. They are all Zip tools... just different compression.

Very interesting stuff Tom! Great site too (punypng.com).

I'm not planning to work on this, but I must sort my curiosity here :-)
I'm not an expert in file formats, my understanding is based mostly in
the common idea that an electronic image is just that, a 3D matrix of
dots (x, y, color) with that info as part of the file's header.

However, if there are so many tools around that offer different
types/levels of lossless compressions (and the files are still
backward-compatible for displaying in browsers and old editors) then
that implies these tools must be injecting the PNG file with the
*actual* code/algorithm that decompresses the picture. Which leads me to
the idea of a security vulnerability (right?). I'm not getting paranoid,
only curious. Is this the way PNG images are crafted?