Wibs2

I must be doing something wrong. All the code snippets and samples I have found on the web for opening an image, resizing it, and saving it out again in the new size, in a variety of formats, have all been quite lengthy.

Here is my code, and it works, so what is wrong

On a form create two picture boxes, 79,79 and 512, 512, and name the first PictureBoxThumb, and the second PictureBox. Add two buttons, the text for Button1 is Load Image, and the text for Button2 is Save Resized Image. The code is:-

Public Class Form1
Public inImage As Bitmap = New Bitmap("C:\test.jpg", True)
Public outImage As Bitmap
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

PictureBoxThumb.Image = inImage.GetThumbnailImage(79, 79, Nothing, Nothing)
outImage = inImage.GetThumbnailImage(79, 79, Nothing, Nothing)
PictureBox.Image = inImage
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
outImage.Save("C:\test.tif", System.Drawing.Imaging.ImageFormat.Tiff)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class

Maybe just works for thumbnails, I thought, but I then tried changing the thumbnail size from 79,79 to 512,512 and the results were great.

It can't be as easy as this, can it


Re: Visual Basic Express Edition My Simple Image Resize and Save Code: Comments Please

WayneSpangler

Yes. I agree with you it sometimes seems like it is so simple. I have done things with a few lines of code where other have like a 100 lines.

I use the KISS principal.






Re: Visual Basic Express Edition My Simple Image Resize and Save Code: Comments Please

Wibs2

Ok, I'm game, what is the KISS principle

On a another note, the ImageFormat Members include most of the popular image formats, such as bmp, gif, jpg, tif etc. What I cannot find in any of the reference books, or on the msdn site, are the options that are set for each of these formats, for example:

BMP - RGB or RLE encoding
GIF - 87a or 89a, interlaced or non-interlaced
JPG - Standard or progresive encoding, compression factor
TIF - compression standard used, RGB or CMYK

Does anyone know what the defaults used are for each member of ImageFormat, and whether they can be changed or not




Re: Visual Basic Express Edition My Simple Image Resize and Save Code: Comments Please

nobugz

The GDI+ encoders have very limited configurability. Only the TIFF and JPEG encoders are known to accept EncoderParameters. They are poorly documented too. Check these MSDN library articles for EncoderValue and Encoder.

One reason you don't see GetThumbnailImage() used very often is because it is very slow. Roughly four times slower than just using DrawImage() into a bitmap. It is still only a 4 line piece of code, check this thread for reference.





Re: Visual Basic Express Edition My Simple Image Resize and Save Code: Comments Please

JohnWein

If you are willing to accept the default configuration of GDI+, the code required to resize an image and save it in a different format can be quite simple:

Code Snippet

Dim OrigBmp As New Bitmap("C:\Test.jpg")

Dim ConvBmp As New Bitmap(OrigBmp, OrigBmp.Width \ 2, OrigBmp.Height \ 2)

ConvBmp.Save("C:\Test1.tif", Imaging.ImageFormat.Tiff)

Keep It Simple Stupid is a good starting point.





Re: Visual Basic Express Edition My Simple Image Resize and Save Code: Comments Please

nobugz

Yes, John's three liner works. Hey John, you said in another thread you didn't know English very well. How come you know KISS Send me an email, my address is in my profile; |Monkeytail| = @