Reputation: 18295
How do you convert a System.Drawing.Bitmap
image to another type of image? I tried CType, which failed, since I do not know the type for .png or .jpg. I cannot find it anywhere on google either.
What is the most efficient method to do this while keeping the quality of the image as high as possible?
Upvotes: 7
Views: 14374
Reputation: 71
Here is how to convert an image type without saving to a file in VB.NET You might also be able to port this into C#, but I haven't tested that.
Public Function ConvertImageType(Image As Bitmap, Format As
Drawing.Imaging.ImageFormat) As Byte()
'Declare a new memory stream to write to
Dim SaveStream As New IO.MemoryStream
'Save the bitmap/image to the memory stream in the desired format
Image.Save(SaveStream, Format)
'Extract the file bytes from the memory stream
Dim ConvertedImageBytes() As Byte = SaveStream.ToArray
'Return bytes to caller
Return ConvertedImageBytes
End Function
This method can convert a Bitmap/Image type to the following formats: Jpeg, Png, Bmp, Ico, Gif, Emf, Exif, Tiff and Wmf.
Upvotes: 1
Reputation: 13488
Check out the Drawing.Bitmap.Save() method. I recommend saving as PNG - it's lossless and achieves reasonable compression. See the Imaging.ImageFormat enum - this is how you specify the image type you want.
Upvotes: 3
Reputation: 66122
The system.Drawing.Bitmap class can handle opening and saving any kind of bitmap image, including JPG, PNG, GIF, BMP, and others.
To save an already opened file as a different format you can use the save method as so
MyImage.Save("ImageName.jpg",System.Drawing.Imaging.ImageFormat.Jpeg)
The class name of Bitmp refers more specifically to the general concept of storing a picture as a series of coloured pixels rather then the actual format of BMP, which is one possible way to store the representation of the pixels that make up an image.
Upvotes: 9