Introduction
This isn't particularly unique, but most of the examples I've found are either seriously flawed or wrap the same sort of thing up with a ton of extra functions your programs may not need. This one tries to trap errors and ensure the release of handles to help avoid memory leaks and other issues. It isn't perfect, and the comments cover some of the extra things that could be done.
The GPSP class (GDI Plus Scale Picture) makes use of GDI Plus Flat API calls to convert a bitmap StdPicture to a new scaled StdPicture. It can be used up "upscale" as well but its main purpose is downscaling. It can be used to create ListView "thumbnail" images but it is based on a larger project related to user interface DPI scaling. That's probably its more general use case.
Since it offers StdPicture to StdPicture operations, in most cases you'll be limited to BMP, JPEG, and GIF images. It has no support for icon StdPicture and makes no effort to preserve transparency anyway. Icons can be scaled better by using a LoadIconWithScaleDown() API call instead anyway.
By doing StdPicture to StdPicture operations this class should be easy to use even for VB6 beginners.
Usage
Basically you just create an instance of GPSP, set a few properties, and call one of its two methods to scale pictures.
Set ScaledPic = GPSP.ByFactor(OrigPic) scales the picture by the Factor property. This is a value like 0.25 for 25%, 1.5 for 150%, etc.
Set ScaledPic = GPSP.ToWidthHeight(OrigPic) scales the picture to specific dimensions. These are the Width and Height properties, in pixels. It also makes use of the KeepAspect property.
If KeepAspect = True then the BackColor property also comes into serious play because the image is scaled and centered within the dimensions.
But due to edge aliasing issues you'll probably always want to assign BackColor some reasonable color. If nothing else you may want to choose something like 50% gray, i.e. &H00808080&, or in most cases the background color of the control you will display the resulting image on.
There is also the Quality property. This accepts values corresponding to the GDI+ InterpolationMode enumeration. These have comments in the code about suggested choices, but which is best varies with the source images and the scaling requested.
Two small sample projects with picture files are attached. Here is one of them:
![Name: sshot2.png
Views: 55
Size: 32.5 KB]()
Requirements
You can drop this class into any project, it has no dependencies aside from GDI+ which we always have now anyway. If you must support something like Windows 95 (?) you should already have the GDI+ redist package. It doesn't require any special compilation process or build steps.
Or you could just extract the code you want and write your own class or use it inline within Forms and such. Some people might just strip back all of the exception handling and raise a generic exception for any failure.
Help appreciated
I'm no GDI+ expert by any means, so if anyone finds bugs I'd love to hear about them. Bug fixes would be even better. ;)
This isn't particularly unique, but most of the examples I've found are either seriously flawed or wrap the same sort of thing up with a ton of extra functions your programs may not need. This one tries to trap errors and ensure the release of handles to help avoid memory leaks and other issues. It isn't perfect, and the comments cover some of the extra things that could be done.
The GPSP class (GDI Plus Scale Picture) makes use of GDI Plus Flat API calls to convert a bitmap StdPicture to a new scaled StdPicture. It can be used up "upscale" as well but its main purpose is downscaling. It can be used to create ListView "thumbnail" images but it is based on a larger project related to user interface DPI scaling. That's probably its more general use case.
Since it offers StdPicture to StdPicture operations, in most cases you'll be limited to BMP, JPEG, and GIF images. It has no support for icon StdPicture and makes no effort to preserve transparency anyway. Icons can be scaled better by using a LoadIconWithScaleDown() API call instead anyway.
By doing StdPicture to StdPicture operations this class should be easy to use even for VB6 beginners.
Usage
Basically you just create an instance of GPSP, set a few properties, and call one of its two methods to scale pictures.
Set ScaledPic = GPSP.ByFactor(OrigPic) scales the picture by the Factor property. This is a value like 0.25 for 25%, 1.5 for 150%, etc.
Set ScaledPic = GPSP.ToWidthHeight(OrigPic) scales the picture to specific dimensions. These are the Width and Height properties, in pixels. It also makes use of the KeepAspect property.
If KeepAspect = True then the BackColor property also comes into serious play because the image is scaled and centered within the dimensions.
But due to edge aliasing issues you'll probably always want to assign BackColor some reasonable color. If nothing else you may want to choose something like 50% gray, i.e. &H00808080&, or in most cases the background color of the control you will display the resulting image on.
There is also the Quality property. This accepts values corresponding to the GDI+ InterpolationMode enumeration. These have comments in the code about suggested choices, but which is best varies with the source images and the scaling requested.
Two small sample projects with picture files are attached. Here is one of them:
Requirements
You can drop this class into any project, it has no dependencies aside from GDI+ which we always have now anyway. If you must support something like Windows 95 (?) you should already have the GDI+ redist package. It doesn't require any special compilation process or build steps.
Or you could just extract the code you want and write your own class or use it inline within Forms and such. Some people might just strip back all of the exception handling and raise a generic exception for any failure.
Help appreciated
I'm no GDI+ expert by any means, so if anyone finds bugs I'd love to hear about them. Bug fixes would be even better. ;)