Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleWrite a method that saves images in an appropriate format depending on the file name's extension in Visual Basic .NET
DescriptionThis example shows how to write a method that saves images in an appropriate format depending on the file name's extension in Visual Basic .NET.
Keywordssave file, save image, save bitmap, save picture, file extension, png, bmp, jpg, jpeg, gif, tiff, Visual Basic .NET, VB.NET
CategoriesGraphics, Files and Directories, VB.NET
 

The Bitmap class provides a Save method that can save an image into a file in many different formats but that method doesn't look at the file name's extension to decide which format to use. For example, you could save an image in JPEG format in a file with a .bmp extension. That could be confusing and might even mess up some applications that tried to read the file.

This example's SaveBitmapUsingExtension method simply examines a file name's extension and then saves a bitmap using the appropriate format.

 
' Save the file with the appropriate format.
' Throw a NotSupportedException if the file
' has an unknown extension.
Public Sub SaveBitmapUsingExtension(ByVal bm As Bitmap, _
    ByVal filename As String)
    Dim extension As String = Path.GetExtension(filename)
    Select Case (extension.ToLower())
        Case ".bmp"
            bm.Save(filename, ImageFormat.Bmp)
        Case ".exif"
            bm.Save(filename, ImageFormat.Exif)
        Case ".gif"
            bm.Save(filename, ImageFormat.Gif)
        Case ".jpg", ".jpeg"
            bm.Save(filename, ImageFormat.Jpeg)
        Case ".png"
            bm.Save(filename, ImageFormat.Png)
        Case ".tif", ".tiff"
            bm.Save(filename, ImageFormat.Tiff)
        Case Else
            Throw New NotSupportedException( _
                "Unknown file extension " & extension)
    End Select
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated