Resize a PNG image keeping transparent

Hi all!
I need to resize a PNG image (with transparency) keeping its transparency…
I use a function like this:

[code]Function GetThumbnail(image as picture, Dimension As Integer) As Picture
dim thumb as Picture
dim ratio as double

’ Return nil if image is Nil
if image = nil then
return nil
end if

'if Dimensione=0 then
'Return image
'end if

’ Create Image Thumbnail
if image.Width>Dimension or image.Height>Dimension Then
if image.Width > image.Height then
ratio = image.Height / image.Width
thumb = New Picture(Dimension, Dimension * ratio, 32)
else
ratio = image.Width / image.Height
thumb = New Picture(Dimension * ratio, Dimension, 32)
end if
Else
thumb = New Picture(image.Width, image.Height, 32)
end if

’ Draw Thumbnail
thumb.Graphics.DrawPicture image, 0, 0, thumb.Width, thumb.Height, 0, 0, image.Width, image.Height

'Return Thumbnail
return thumb
End Function
[/code]

But it returns an image with white background. If I use this

' Draw Thumbnail thumb.Graphics.DrawPicture image.CopyPictureWithMaskMBS, 0, 0, thumb.Width, thumb.Height, 0, 0, image.Width, image.Height
I have an image with black background.

Any help?

Many thanks!

If you’re willing to use a commercial plugin, both MBS and Studio Stable Graphics have functions for resizing pictures and keeping the alpha channel:

http://www.monkeybreadsoftware.net/

http://www.studiostable.com/graphics/stretch-flip-and-rotate

-Brad

To keep the alpha layer in a clone picture, you have to do something like this (’ theClone’ is a Picture object used as the result)

// Save the mask reference but without creating it if it doesn't exist Dim theMask As Picture = theOriginal.Mask( False ) // Remove the cache in the original theOriginal.Mask( False ) = Nil // Draw the picture w/o transparency theClone.Graphics.DrawPicture theOriginal, 0, 0 // Draw the mask if there is one If theMask <> Nil Then theClone.Mask.Graphics.DrawPicture theMask, 0, 0 // Restore the mask in the original picture theOriginal.Mask( False ) = theMask End If // Return the Result Return theClone

Of course, you have to adapt the DrawPicture parameters to your needs and to add some error checking.
the code above hasn’t been tested, it’s just out of my head…
More information about Picture.Mask here.

Also, whatever you do, use the new alpha channel pictures. Your PNGs, whether as project items or opened with Picture.Open, start life as alpha channel pictures, not masked pictures.

Brad, can you give some specifics of what you mean. I have the same problem, if I try to resize a PNG file, I lose the transparency. Assigning the picture object directly retains transparency but of course, doesn’t fit in the allotted area.

Thanks,
Dan

(I tried the Mask code above, but it didn’t work for me.)

Another commercial option: Web Custom Control’s WebImageViewTD lets you set the size on the client. This offloads the work of scaling to the client browser and let’s you change size without re-downloading a new image. The browser will retain transparency when scaling a PNG.

[quote=82355:@Dan Harding]Brad, can you give some specifics of what you mean. I have the same problem, if I try to resize a PNG file, I lose the transparency. Assigning the picture object directly retains transparency but of course, doesn’t fit in the allotted area.
[/quote]

I’d just suggest using one of the plugins I mentioned rather than trying to mess with masks. Plugins are way faster. They account for the alpha channel.

-Brad

This is a piece of cake. Create a new picture with no depth. It will be transparent. Draw the png to it at the size you desire. You are done. Can be done in two lines of code.

[quote=82556:@Thom McGrath]Create a new picture with no depth[/quote] +1

Can’t believe we all missed that. Guess it pays to read the documentation: To create a Picture with an alpha channel, use the new constructor that omits the Depth parameter:

Thanks Thom!

Another method is to use the WebHTMLViewer to display your picture.

Create an HTML string with the style information in it an load the page in. This resizes using the browser which doesn’t pixelate the picture and resizes automatically to the size of viewer.

E.g.:

Dim HTML As String
Dim Style As String

Style = “width:100%; height: 100%;opacity: 0.4;”

HTML = “

myViewer.LoadPage(HTML)