Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Loading and saving bitmap images

2008-12-13

Instances of the Bitmap class (System.Drawing.Bitmap) represent a bitmapped image. To load an image, just pass the filename to the Bitmap constructor. This example loads a jpeg image from the “my pictures” folder in the current user’s local storage.

Dim MyPics As String = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
Dim B As New System.Drawing.Bitmap(MyPics & “\duck.jpg”)

The supported formats are BMP, GIF, JPEG, PNG and TIFF.

To save the image, just call the function Bitmap.Save of the Bitmap instance, and specify the desired format by passing one of the read only properties in the ImageFormat class as the second argument. This code is saves the loaded image in the Bitmap instance in five different formats.

B.Save(MyPics & “\testsave.jpg”, Imaging.ImageFormat.Jpeg)
B.Save(MyPics & “\testsave.bmp”, Imaging.ImageFormat.Bmp)
B.Save(MyPics & “\testsave.gif”, Imaging.ImageFormat.Gif)
B.Save(MyPics & “\testsave.tif”, Imaging.ImageFormat.Tiff)
B.Save(MyPics & “\testsave.png”, Imaging.ImageFormat.Png)

In some cases, you want to pass arguments to control compression and quality, for example when you save a JPEG image. The Save function has an overloaded version that can take codec parameters as an argument. To use that overload, I need a JPEG codec that can be found in the vector that is returned from the ImageCodecInfo.GetImageEncoders function. This is one piece of code that really would look better if Linq is used, but I do this in Visual Basic 8, so Linq is not available to me.

Dim Codecs() As Imaging.ImageCodecInfo = Imaging.ImageCodecInfo.GetImageEncoders()
For Each Codec As Imaging.ImageCodecInfo In Codecs
If Codec.MimeType = “image/jpeg” Then
‘More code here
Exit For
End If
Next

At the remark “More code here”, I will now add code to actually save the image. This is the complete code for this example:

Dim Codecs() As Imaging.ImageCodecInfo = Imaging.ImageCodecInfo.GetImageEncoders()
For Each Codec As Imaging.ImageCodecInfo In Codecs
If Codec.MimeType = “image/jpeg” Then
‘Create the desiered parameters in a list
Dim HighCompression As New Imaging.EncoderParameter(Imaging.Encoder.Quality, 0)
Dim AllMyParameters As New Imaging.EncoderParameters(1)
AllMyParameters.Param(0) = HighCompression
‘Save the image using the codec and the parameter(s)
B.Save(MyPics & “\high_compression.jpg”, Codec, AllMyParameters)
Exit For
End If
Next

If you run this, you should end up with a picture of very poor quality. The compression (0) is the highest possible, and the file is as small as it can be.

The quality parameter can be a value from 0 to 100, so to get the best possible quality (lowest compression), change the quality parameter to 100, like this:

Dim HighCompression As New Imaging.EncoderParameter(Imaging.Encoder.Quality, 100)

This is the basics in loading and saving bitmap images in .NET 2.0.

Categories: Visual Basic 8

Tags: Bitmap, JPG

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips