Image conversion in Windows Forms
I hope I get this in the right area, super new here.
I am working on image conversion in windows forms.
My code is:
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class ImageConversion
Private currentFile As String
Private image As Image
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
With OpenFile
.Title = "Open Image File"
.Filter = "Bitmap Files|*.bmp" +
"|Enhanced Windows MetaFile|*.emf" +
"|Exchangeable Image File|*.exif" +
"|Gif Files|*.gif" +
"|Icons|*.ico" +
"|JPEG Files|*.jpg" +
"|PNG Files|*.png" +
"|TIFF Files|*.tif" +
"|Windows MetaFile|*.wmf"
.DefaultExt = "jpg"
.FilterIndex = 6
.FileName = ""
End With
OpenFile.ShowDialog()
If OpenFile.FileName = "" Then
Return
End If
currentFile = OpenFile.FileName.ToString()
image = Image.FromFile(OpenFile.FileName) .
PictureBox1.Image = image
Me.Text = "Image Conversion -" & OpenFile.SafeFileName.ToString()
End Sub
Private Sub BitmapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BitmapToolStripMenuItem.Click
Dim newName As String = System.IO.Path.GetFileNameWithoutExtension(currentFile)
newName = newName + ".bmp"
If SaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
Image.Save(SaveFile.FileName, ImageFormat.Bmp)
Catch ex As Exception
MessageBox.Show("Failed to save image to bitmap.", "Error" & ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
MessageBox.Show("Image File Saved To" + SaveFile.FileName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Class
This is then repeated for each additional file type. Whenever going to convert my file type doesn't change to the desired conversion nor does the save option give me the choice to pick and it actually doesn't save it as anything other than file type: File.
Attached is a snippet of what happens when the save pops up.
I feel like there is something simple I am missing here.
vb.net winforms image-conversion
|
show 6 more comments
I hope I get this in the right area, super new here.
I am working on image conversion in windows forms.
My code is:
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class ImageConversion
Private currentFile As String
Private image As Image
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
With OpenFile
.Title = "Open Image File"
.Filter = "Bitmap Files|*.bmp" +
"|Enhanced Windows MetaFile|*.emf" +
"|Exchangeable Image File|*.exif" +
"|Gif Files|*.gif" +
"|Icons|*.ico" +
"|JPEG Files|*.jpg" +
"|PNG Files|*.png" +
"|TIFF Files|*.tif" +
"|Windows MetaFile|*.wmf"
.DefaultExt = "jpg"
.FilterIndex = 6
.FileName = ""
End With
OpenFile.ShowDialog()
If OpenFile.FileName = "" Then
Return
End If
currentFile = OpenFile.FileName.ToString()
image = Image.FromFile(OpenFile.FileName) .
PictureBox1.Image = image
Me.Text = "Image Conversion -" & OpenFile.SafeFileName.ToString()
End Sub
Private Sub BitmapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BitmapToolStripMenuItem.Click
Dim newName As String = System.IO.Path.GetFileNameWithoutExtension(currentFile)
newName = newName + ".bmp"
If SaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
Image.Save(SaveFile.FileName, ImageFormat.Bmp)
Catch ex As Exception
MessageBox.Show("Failed to save image to bitmap.", "Error" & ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
MessageBox.Show("Image File Saved To" + SaveFile.FileName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Class
This is then repeated for each additional file type. Whenever going to convert my file type doesn't change to the desired conversion nor does the save option give me the choice to pick and it actually doesn't save it as anything other than file type: File.
Attached is a snippet of what happens when the save pops up.
I feel like there is something simple I am missing here.
vb.net winforms image-conversion
You should make use of theImage Converter
Class
– preciousbetine
Nov 24 '18 at 15:11
1
@Jimi : Actually he isn't just changing the extension. His code loads the selected image into memory (which always uses the same format: an uncompressed memory bitmap) and then saves it, specifying the format to use. GDI+ takes care of the rest.
– Visual Vincent
Nov 24 '18 at 19:16
1
@preciousbetine : TheImageConverter
class is used to convert anImage
to/from different data types. To convert an image into a different format you've got to use theBitmap.Save(String, ImageFormat)
overload.
– Visual Vincent
Nov 24 '18 at 19:21
@VisualVincent yeah that works.
– preciousbetine
Nov 24 '18 at 19:24
@TravisRoberts : You set theFilter
property for theOpenFile
dialog but you never seem to do the same for theSaveFile
dialog (unless you do so through the designer). Is this the case?
– Visual Vincent
Nov 24 '18 at 19:29
|
show 6 more comments
I hope I get this in the right area, super new here.
I am working on image conversion in windows forms.
My code is:
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class ImageConversion
Private currentFile As String
Private image As Image
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
With OpenFile
.Title = "Open Image File"
.Filter = "Bitmap Files|*.bmp" +
"|Enhanced Windows MetaFile|*.emf" +
"|Exchangeable Image File|*.exif" +
"|Gif Files|*.gif" +
"|Icons|*.ico" +
"|JPEG Files|*.jpg" +
"|PNG Files|*.png" +
"|TIFF Files|*.tif" +
"|Windows MetaFile|*.wmf"
.DefaultExt = "jpg"
.FilterIndex = 6
.FileName = ""
End With
OpenFile.ShowDialog()
If OpenFile.FileName = "" Then
Return
End If
currentFile = OpenFile.FileName.ToString()
image = Image.FromFile(OpenFile.FileName) .
PictureBox1.Image = image
Me.Text = "Image Conversion -" & OpenFile.SafeFileName.ToString()
End Sub
Private Sub BitmapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BitmapToolStripMenuItem.Click
Dim newName As String = System.IO.Path.GetFileNameWithoutExtension(currentFile)
newName = newName + ".bmp"
If SaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
Image.Save(SaveFile.FileName, ImageFormat.Bmp)
Catch ex As Exception
MessageBox.Show("Failed to save image to bitmap.", "Error" & ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
MessageBox.Show("Image File Saved To" + SaveFile.FileName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Class
This is then repeated for each additional file type. Whenever going to convert my file type doesn't change to the desired conversion nor does the save option give me the choice to pick and it actually doesn't save it as anything other than file type: File.
Attached is a snippet of what happens when the save pops up.
I feel like there is something simple I am missing here.
vb.net winforms image-conversion
I hope I get this in the right area, super new here.
I am working on image conversion in windows forms.
My code is:
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class ImageConversion
Private currentFile As String
Private image As Image
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
With OpenFile
.Title = "Open Image File"
.Filter = "Bitmap Files|*.bmp" +
"|Enhanced Windows MetaFile|*.emf" +
"|Exchangeable Image File|*.exif" +
"|Gif Files|*.gif" +
"|Icons|*.ico" +
"|JPEG Files|*.jpg" +
"|PNG Files|*.png" +
"|TIFF Files|*.tif" +
"|Windows MetaFile|*.wmf"
.DefaultExt = "jpg"
.FilterIndex = 6
.FileName = ""
End With
OpenFile.ShowDialog()
If OpenFile.FileName = "" Then
Return
End If
currentFile = OpenFile.FileName.ToString()
image = Image.FromFile(OpenFile.FileName) .
PictureBox1.Image = image
Me.Text = "Image Conversion -" & OpenFile.SafeFileName.ToString()
End Sub
Private Sub BitmapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BitmapToolStripMenuItem.Click
Dim newName As String = System.IO.Path.GetFileNameWithoutExtension(currentFile)
newName = newName + ".bmp"
If SaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
Image.Save(SaveFile.FileName, ImageFormat.Bmp)
Catch ex As Exception
MessageBox.Show("Failed to save image to bitmap.", "Error" & ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
MessageBox.Show("Image File Saved To" + SaveFile.FileName.ToString(), "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Class
This is then repeated for each additional file type. Whenever going to convert my file type doesn't change to the desired conversion nor does the save option give me the choice to pick and it actually doesn't save it as anything other than file type: File.
Attached is a snippet of what happens when the save pops up.
I feel like there is something simple I am missing here.
vb.net winforms image-conversion
vb.net winforms image-conversion
edited Nov 24 '18 at 15:10
Jimi
7,40241833
7,40241833
asked Nov 24 '18 at 4:18
Travis RobertsTravis Roberts
111
111
You should make use of theImage Converter
Class
– preciousbetine
Nov 24 '18 at 15:11
1
@Jimi : Actually he isn't just changing the extension. His code loads the selected image into memory (which always uses the same format: an uncompressed memory bitmap) and then saves it, specifying the format to use. GDI+ takes care of the rest.
– Visual Vincent
Nov 24 '18 at 19:16
1
@preciousbetine : TheImageConverter
class is used to convert anImage
to/from different data types. To convert an image into a different format you've got to use theBitmap.Save(String, ImageFormat)
overload.
– Visual Vincent
Nov 24 '18 at 19:21
@VisualVincent yeah that works.
– preciousbetine
Nov 24 '18 at 19:24
@TravisRoberts : You set theFilter
property for theOpenFile
dialog but you never seem to do the same for theSaveFile
dialog (unless you do so through the designer). Is this the case?
– Visual Vincent
Nov 24 '18 at 19:29
|
show 6 more comments
You should make use of theImage Converter
Class
– preciousbetine
Nov 24 '18 at 15:11
1
@Jimi : Actually he isn't just changing the extension. His code loads the selected image into memory (which always uses the same format: an uncompressed memory bitmap) and then saves it, specifying the format to use. GDI+ takes care of the rest.
– Visual Vincent
Nov 24 '18 at 19:16
1
@preciousbetine : TheImageConverter
class is used to convert anImage
to/from different data types. To convert an image into a different format you've got to use theBitmap.Save(String, ImageFormat)
overload.
– Visual Vincent
Nov 24 '18 at 19:21
@VisualVincent yeah that works.
– preciousbetine
Nov 24 '18 at 19:24
@TravisRoberts : You set theFilter
property for theOpenFile
dialog but you never seem to do the same for theSaveFile
dialog (unless you do so through the designer). Is this the case?
– Visual Vincent
Nov 24 '18 at 19:29
You should make use of the
Image Converter
Class– preciousbetine
Nov 24 '18 at 15:11
You should make use of the
Image Converter
Class– preciousbetine
Nov 24 '18 at 15:11
1
1
@Jimi : Actually he isn't just changing the extension. His code loads the selected image into memory (which always uses the same format: an uncompressed memory bitmap) and then saves it, specifying the format to use. GDI+ takes care of the rest.
– Visual Vincent
Nov 24 '18 at 19:16
@Jimi : Actually he isn't just changing the extension. His code loads the selected image into memory (which always uses the same format: an uncompressed memory bitmap) and then saves it, specifying the format to use. GDI+ takes care of the rest.
– Visual Vincent
Nov 24 '18 at 19:16
1
1
@preciousbetine : The
ImageConverter
class is used to convert an Image
to/from different data types. To convert an image into a different format you've got to use the Bitmap.Save(String, ImageFormat)
overload.– Visual Vincent
Nov 24 '18 at 19:21
@preciousbetine : The
ImageConverter
class is used to convert an Image
to/from different data types. To convert an image into a different format you've got to use the Bitmap.Save(String, ImageFormat)
overload.– Visual Vincent
Nov 24 '18 at 19:21
@VisualVincent yeah that works.
– preciousbetine
Nov 24 '18 at 19:24
@VisualVincent yeah that works.
– preciousbetine
Nov 24 '18 at 19:24
@TravisRoberts : You set the
Filter
property for the OpenFile
dialog but you never seem to do the same for the SaveFile
dialog (unless you do so through the designer). Is this the case?– Visual Vincent
Nov 24 '18 at 19:29
@TravisRoberts : You set the
Filter
property for the OpenFile
dialog but you never seem to do the same for the SaveFile
dialog (unless you do so through the designer). Is this the case?– Visual Vincent
Nov 24 '18 at 19:29
|
show 6 more comments
1 Answer
1
active
oldest
votes
I've modified your original class to add some missing features:
- The selected File Format returned by the
SaveFileDialog()
- The selection of Filters in
SaveFileDialog()
is reduced to the formats that Image.Save() actually supports
IDisposable
support, used to dispose of the Bitmap that is currently loaded- Some others details you can find in the notes and in the class code
This is now a stand-alone class that can be used in other contexts (there's no reference to specific controls or methods:
Sample usage:
Initialize the ImageConversion
class (in a Form's constructor or where you think is appropriate):
Public Partial Class Form1
Public imgConversion As ImageConversion = New ImageConversion()
'(...)
End Class
Your handlers can be modified like this:
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose()
Dim NewBitmapFile = imgConversion.OpenFile()
If NewBitmapFile.OpenedBitmap IsNot Nothing Then
PictureBox1.Image = NewBitmapFile.OpenedBitmap
Me.Text = NewBitmapFile.FileName
End If
End Sub
Private Sub BitmapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BitmapToolStripMenuItem.Click
Dim SavedFile = imgConversion.SaveFileFormat()
If SavedFile.ErrorMessage <> String.Empty Then
MessageBox.Show("Failed to save image to Bitmap.", "Error" & SavedFile.ErrorMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf SavedFile.FileName <> String.Empty Then
MessageBox.Show("Image File saved to: " + SavedFile.FileName, "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Dispose of the ImageConversion
class when the Form is closed, this will free the last Image object used:
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
imgConversion.Dispose()
End Sub
To note:
Since you are using a method that returns a
Bitmap
object and this object is then assigned to aPictureBox.Image
property, you need to dispose of the previous Image, if any, before assigning a new one. Otherwise this lost object will linger on more that it's desired and it this will hamper you app.The Filters, in the
SaveFileDialog
, have beed reduced to theImageFormats
that are actually supported by GDI+ when creating a new Bitmap.Icon
,WMF
,EMF
andExif
are not supported and the resulting Image will be aPNG
file format, the default GDI+ format.
The first three can be created with other means, but this is a broad matter and can't be addressed here.
A couple of methods return a ValueTuple, in the form of
ValueTuple(Of T1, T2)
.
I'm not sure your VB.Net version supports this return type and form.
If not, the method can be modified to returnByRef
results or a specialized public object (a sub-class ofImageConversion
) that holds the required informations (probably preferable).
Public Function OpenFile() As (OpenedBitmap As Image, FileName As String)
'(...)
Dim result = OpenFile()
Dim img As Bitmap = result.OpenedBitmap
Dim fName As String = result.FileName
The ImageConversion
class:
Imports System.Drawing.Imaging
Imports System.Globalization
Imports System.IO
Public Class ImageConversion
Implements IDisposable
Private IsDisposed As Boolean = False
Private CurrentFile As String
Private CurrentBitmap As Image
Private Enum FilterType
OpenFile
SaveFile
End Enum
Public Function OpenFile() As (OpenedBitmap As Image, FileName As String)
Using OFD As OpenFileDialog = New OpenFileDialog()
With OFD
.CheckFileExists = True
.CheckPathExists = True
.RestoreDirectory = True
.Title = "Open Image File"
.Filter = GetFileFilters(FilterType.OpenFile)
.FilterIndex = 4
.FileName = ""
End With
If OFD.ShowDialog() = DialogResult.Cancel Then Return Nothing
CurrentFile = OFD.FileName
If CurrentBitmap IsNot Nothing Then CurrentBitmap.Dispose()
CurrentBitmap = CType(Image.FromFile(CurrentFile).Clone(), Bitmap)
End Using
Return (CurrentBitmap, CurrentFile)
End Function
Public Function SaveFileFormat() As (FileName As String, ErrorMessage As String)
Dim NewFileName As String = Path.GetFileNameWithoutExtension(CurrentFile)
Dim ErrorMessage As String = String.Empty
Using SFD As SaveFileDialog = New SaveFileDialog()
With SFD
.AddExtension = True
.ValidateNames = True
.CheckPathExists = True
.RestoreDirectory = True
.Title = "Save Image File"
.Filter = GetFileFilters(FilterType.SaveFile)
.FileName = NewFileName
End With
If SFD.ShowDialog() = DialogResult.Cancel Then Return (String.Empty, String.Empty)
Try
NewFileName = SFD.FileName
Dim imgFormat As ImageFormat = GetImageFormat(NewFileName)
CurrentBitmap.Save(NewFileName, imgFormat)
Catch ex As IOException
NewFileName = String.Empty
End Try
End Using
Return (NewFileName, ErrorMessage)
End Function
Private Function GetImageFormat(FileName As String) As ImageFormat
Dim fileType As String = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Path.GetExtension(FileName).Remove(0, 1).ToLower())
If fileType = "Tif" Then fileType = "Tiff"
If fileType = "Jpg" Then fileType = "Jpeg"
Dim imgFormat As ImageFormat = New ImageFormat(New Guid())
Return DirectCast(imgFormat.GetType().GetProperty(fileType).GetValue(imgFormat), ImageFormat)
End Function
Private Function GetFileFilters(Filter As FilterType) As String
Dim Filters As String() = {
"BMP Files|*.bmp", "|GIF Files|*.gif", "|JPEG Files|*.jpg",
"|PNG Files|*.png", "|TIFF Files|*.tif", "|Enhanced Windows MetaFile|*.emf",
"|Exchangeable Image File|*.exif", "|Icons|*.ico", "|Windows MetaFile|*.wmf"
}
Select Case Filter
Case FilterType.OpenFile
Return String.Join("", Filters)
Case FilterType.SaveFile
Return String.Join("", Filters.Take(5))
Case Else
Return String.Empty
End Select
End Function
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Sub Dispose(disposing As Boolean)
If IsDisposed Then Return
If disposing Then
If CurrentBitmap IsNot Nothing Then
CurrentBitmap.Dispose()
End If
End If
IsDisposed = True
End Sub
End Class
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455103%2fimage-conversion-in-windows-forms%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've modified your original class to add some missing features:
- The selected File Format returned by the
SaveFileDialog()
- The selection of Filters in
SaveFileDialog()
is reduced to the formats that Image.Save() actually supports
IDisposable
support, used to dispose of the Bitmap that is currently loaded- Some others details you can find in the notes and in the class code
This is now a stand-alone class that can be used in other contexts (there's no reference to specific controls or methods:
Sample usage:
Initialize the ImageConversion
class (in a Form's constructor or where you think is appropriate):
Public Partial Class Form1
Public imgConversion As ImageConversion = New ImageConversion()
'(...)
End Class
Your handlers can be modified like this:
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose()
Dim NewBitmapFile = imgConversion.OpenFile()
If NewBitmapFile.OpenedBitmap IsNot Nothing Then
PictureBox1.Image = NewBitmapFile.OpenedBitmap
Me.Text = NewBitmapFile.FileName
End If
End Sub
Private Sub BitmapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BitmapToolStripMenuItem.Click
Dim SavedFile = imgConversion.SaveFileFormat()
If SavedFile.ErrorMessage <> String.Empty Then
MessageBox.Show("Failed to save image to Bitmap.", "Error" & SavedFile.ErrorMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf SavedFile.FileName <> String.Empty Then
MessageBox.Show("Image File saved to: " + SavedFile.FileName, "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Dispose of the ImageConversion
class when the Form is closed, this will free the last Image object used:
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
imgConversion.Dispose()
End Sub
To note:
Since you are using a method that returns a
Bitmap
object and this object is then assigned to aPictureBox.Image
property, you need to dispose of the previous Image, if any, before assigning a new one. Otherwise this lost object will linger on more that it's desired and it this will hamper you app.The Filters, in the
SaveFileDialog
, have beed reduced to theImageFormats
that are actually supported by GDI+ when creating a new Bitmap.Icon
,WMF
,EMF
andExif
are not supported and the resulting Image will be aPNG
file format, the default GDI+ format.
The first three can be created with other means, but this is a broad matter and can't be addressed here.
A couple of methods return a ValueTuple, in the form of
ValueTuple(Of T1, T2)
.
I'm not sure your VB.Net version supports this return type and form.
If not, the method can be modified to returnByRef
results or a specialized public object (a sub-class ofImageConversion
) that holds the required informations (probably preferable).
Public Function OpenFile() As (OpenedBitmap As Image, FileName As String)
'(...)
Dim result = OpenFile()
Dim img As Bitmap = result.OpenedBitmap
Dim fName As String = result.FileName
The ImageConversion
class:
Imports System.Drawing.Imaging
Imports System.Globalization
Imports System.IO
Public Class ImageConversion
Implements IDisposable
Private IsDisposed As Boolean = False
Private CurrentFile As String
Private CurrentBitmap As Image
Private Enum FilterType
OpenFile
SaveFile
End Enum
Public Function OpenFile() As (OpenedBitmap As Image, FileName As String)
Using OFD As OpenFileDialog = New OpenFileDialog()
With OFD
.CheckFileExists = True
.CheckPathExists = True
.RestoreDirectory = True
.Title = "Open Image File"
.Filter = GetFileFilters(FilterType.OpenFile)
.FilterIndex = 4
.FileName = ""
End With
If OFD.ShowDialog() = DialogResult.Cancel Then Return Nothing
CurrentFile = OFD.FileName
If CurrentBitmap IsNot Nothing Then CurrentBitmap.Dispose()
CurrentBitmap = CType(Image.FromFile(CurrentFile).Clone(), Bitmap)
End Using
Return (CurrentBitmap, CurrentFile)
End Function
Public Function SaveFileFormat() As (FileName As String, ErrorMessage As String)
Dim NewFileName As String = Path.GetFileNameWithoutExtension(CurrentFile)
Dim ErrorMessage As String = String.Empty
Using SFD As SaveFileDialog = New SaveFileDialog()
With SFD
.AddExtension = True
.ValidateNames = True
.CheckPathExists = True
.RestoreDirectory = True
.Title = "Save Image File"
.Filter = GetFileFilters(FilterType.SaveFile)
.FileName = NewFileName
End With
If SFD.ShowDialog() = DialogResult.Cancel Then Return (String.Empty, String.Empty)
Try
NewFileName = SFD.FileName
Dim imgFormat As ImageFormat = GetImageFormat(NewFileName)
CurrentBitmap.Save(NewFileName, imgFormat)
Catch ex As IOException
NewFileName = String.Empty
End Try
End Using
Return (NewFileName, ErrorMessage)
End Function
Private Function GetImageFormat(FileName As String) As ImageFormat
Dim fileType As String = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Path.GetExtension(FileName).Remove(0, 1).ToLower())
If fileType = "Tif" Then fileType = "Tiff"
If fileType = "Jpg" Then fileType = "Jpeg"
Dim imgFormat As ImageFormat = New ImageFormat(New Guid())
Return DirectCast(imgFormat.GetType().GetProperty(fileType).GetValue(imgFormat), ImageFormat)
End Function
Private Function GetFileFilters(Filter As FilterType) As String
Dim Filters As String() = {
"BMP Files|*.bmp", "|GIF Files|*.gif", "|JPEG Files|*.jpg",
"|PNG Files|*.png", "|TIFF Files|*.tif", "|Enhanced Windows MetaFile|*.emf",
"|Exchangeable Image File|*.exif", "|Icons|*.ico", "|Windows MetaFile|*.wmf"
}
Select Case Filter
Case FilterType.OpenFile
Return String.Join("", Filters)
Case FilterType.SaveFile
Return String.Join("", Filters.Take(5))
Case Else
Return String.Empty
End Select
End Function
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Sub Dispose(disposing As Boolean)
If IsDisposed Then Return
If disposing Then
If CurrentBitmap IsNot Nothing Then
CurrentBitmap.Dispose()
End If
End If
IsDisposed = True
End Sub
End Class
add a comment |
I've modified your original class to add some missing features:
- The selected File Format returned by the
SaveFileDialog()
- The selection of Filters in
SaveFileDialog()
is reduced to the formats that Image.Save() actually supports
IDisposable
support, used to dispose of the Bitmap that is currently loaded- Some others details you can find in the notes and in the class code
This is now a stand-alone class that can be used in other contexts (there's no reference to specific controls or methods:
Sample usage:
Initialize the ImageConversion
class (in a Form's constructor or where you think is appropriate):
Public Partial Class Form1
Public imgConversion As ImageConversion = New ImageConversion()
'(...)
End Class
Your handlers can be modified like this:
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose()
Dim NewBitmapFile = imgConversion.OpenFile()
If NewBitmapFile.OpenedBitmap IsNot Nothing Then
PictureBox1.Image = NewBitmapFile.OpenedBitmap
Me.Text = NewBitmapFile.FileName
End If
End Sub
Private Sub BitmapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BitmapToolStripMenuItem.Click
Dim SavedFile = imgConversion.SaveFileFormat()
If SavedFile.ErrorMessage <> String.Empty Then
MessageBox.Show("Failed to save image to Bitmap.", "Error" & SavedFile.ErrorMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf SavedFile.FileName <> String.Empty Then
MessageBox.Show("Image File saved to: " + SavedFile.FileName, "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Dispose of the ImageConversion
class when the Form is closed, this will free the last Image object used:
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
imgConversion.Dispose()
End Sub
To note:
Since you are using a method that returns a
Bitmap
object and this object is then assigned to aPictureBox.Image
property, you need to dispose of the previous Image, if any, before assigning a new one. Otherwise this lost object will linger on more that it's desired and it this will hamper you app.The Filters, in the
SaveFileDialog
, have beed reduced to theImageFormats
that are actually supported by GDI+ when creating a new Bitmap.Icon
,WMF
,EMF
andExif
are not supported and the resulting Image will be aPNG
file format, the default GDI+ format.
The first three can be created with other means, but this is a broad matter and can't be addressed here.
A couple of methods return a ValueTuple, in the form of
ValueTuple(Of T1, T2)
.
I'm not sure your VB.Net version supports this return type and form.
If not, the method can be modified to returnByRef
results or a specialized public object (a sub-class ofImageConversion
) that holds the required informations (probably preferable).
Public Function OpenFile() As (OpenedBitmap As Image, FileName As String)
'(...)
Dim result = OpenFile()
Dim img As Bitmap = result.OpenedBitmap
Dim fName As String = result.FileName
The ImageConversion
class:
Imports System.Drawing.Imaging
Imports System.Globalization
Imports System.IO
Public Class ImageConversion
Implements IDisposable
Private IsDisposed As Boolean = False
Private CurrentFile As String
Private CurrentBitmap As Image
Private Enum FilterType
OpenFile
SaveFile
End Enum
Public Function OpenFile() As (OpenedBitmap As Image, FileName As String)
Using OFD As OpenFileDialog = New OpenFileDialog()
With OFD
.CheckFileExists = True
.CheckPathExists = True
.RestoreDirectory = True
.Title = "Open Image File"
.Filter = GetFileFilters(FilterType.OpenFile)
.FilterIndex = 4
.FileName = ""
End With
If OFD.ShowDialog() = DialogResult.Cancel Then Return Nothing
CurrentFile = OFD.FileName
If CurrentBitmap IsNot Nothing Then CurrentBitmap.Dispose()
CurrentBitmap = CType(Image.FromFile(CurrentFile).Clone(), Bitmap)
End Using
Return (CurrentBitmap, CurrentFile)
End Function
Public Function SaveFileFormat() As (FileName As String, ErrorMessage As String)
Dim NewFileName As String = Path.GetFileNameWithoutExtension(CurrentFile)
Dim ErrorMessage As String = String.Empty
Using SFD As SaveFileDialog = New SaveFileDialog()
With SFD
.AddExtension = True
.ValidateNames = True
.CheckPathExists = True
.RestoreDirectory = True
.Title = "Save Image File"
.Filter = GetFileFilters(FilterType.SaveFile)
.FileName = NewFileName
End With
If SFD.ShowDialog() = DialogResult.Cancel Then Return (String.Empty, String.Empty)
Try
NewFileName = SFD.FileName
Dim imgFormat As ImageFormat = GetImageFormat(NewFileName)
CurrentBitmap.Save(NewFileName, imgFormat)
Catch ex As IOException
NewFileName = String.Empty
End Try
End Using
Return (NewFileName, ErrorMessage)
End Function
Private Function GetImageFormat(FileName As String) As ImageFormat
Dim fileType As String = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Path.GetExtension(FileName).Remove(0, 1).ToLower())
If fileType = "Tif" Then fileType = "Tiff"
If fileType = "Jpg" Then fileType = "Jpeg"
Dim imgFormat As ImageFormat = New ImageFormat(New Guid())
Return DirectCast(imgFormat.GetType().GetProperty(fileType).GetValue(imgFormat), ImageFormat)
End Function
Private Function GetFileFilters(Filter As FilterType) As String
Dim Filters As String() = {
"BMP Files|*.bmp", "|GIF Files|*.gif", "|JPEG Files|*.jpg",
"|PNG Files|*.png", "|TIFF Files|*.tif", "|Enhanced Windows MetaFile|*.emf",
"|Exchangeable Image File|*.exif", "|Icons|*.ico", "|Windows MetaFile|*.wmf"
}
Select Case Filter
Case FilterType.OpenFile
Return String.Join("", Filters)
Case FilterType.SaveFile
Return String.Join("", Filters.Take(5))
Case Else
Return String.Empty
End Select
End Function
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Sub Dispose(disposing As Boolean)
If IsDisposed Then Return
If disposing Then
If CurrentBitmap IsNot Nothing Then
CurrentBitmap.Dispose()
End If
End If
IsDisposed = True
End Sub
End Class
add a comment |
I've modified your original class to add some missing features:
- The selected File Format returned by the
SaveFileDialog()
- The selection of Filters in
SaveFileDialog()
is reduced to the formats that Image.Save() actually supports
IDisposable
support, used to dispose of the Bitmap that is currently loaded- Some others details you can find in the notes and in the class code
This is now a stand-alone class that can be used in other contexts (there's no reference to specific controls or methods:
Sample usage:
Initialize the ImageConversion
class (in a Form's constructor or where you think is appropriate):
Public Partial Class Form1
Public imgConversion As ImageConversion = New ImageConversion()
'(...)
End Class
Your handlers can be modified like this:
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose()
Dim NewBitmapFile = imgConversion.OpenFile()
If NewBitmapFile.OpenedBitmap IsNot Nothing Then
PictureBox1.Image = NewBitmapFile.OpenedBitmap
Me.Text = NewBitmapFile.FileName
End If
End Sub
Private Sub BitmapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BitmapToolStripMenuItem.Click
Dim SavedFile = imgConversion.SaveFileFormat()
If SavedFile.ErrorMessage <> String.Empty Then
MessageBox.Show("Failed to save image to Bitmap.", "Error" & SavedFile.ErrorMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf SavedFile.FileName <> String.Empty Then
MessageBox.Show("Image File saved to: " + SavedFile.FileName, "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Dispose of the ImageConversion
class when the Form is closed, this will free the last Image object used:
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
imgConversion.Dispose()
End Sub
To note:
Since you are using a method that returns a
Bitmap
object and this object is then assigned to aPictureBox.Image
property, you need to dispose of the previous Image, if any, before assigning a new one. Otherwise this lost object will linger on more that it's desired and it this will hamper you app.The Filters, in the
SaveFileDialog
, have beed reduced to theImageFormats
that are actually supported by GDI+ when creating a new Bitmap.Icon
,WMF
,EMF
andExif
are not supported and the resulting Image will be aPNG
file format, the default GDI+ format.
The first three can be created with other means, but this is a broad matter and can't be addressed here.
A couple of methods return a ValueTuple, in the form of
ValueTuple(Of T1, T2)
.
I'm not sure your VB.Net version supports this return type and form.
If not, the method can be modified to returnByRef
results or a specialized public object (a sub-class ofImageConversion
) that holds the required informations (probably preferable).
Public Function OpenFile() As (OpenedBitmap As Image, FileName As String)
'(...)
Dim result = OpenFile()
Dim img As Bitmap = result.OpenedBitmap
Dim fName As String = result.FileName
The ImageConversion
class:
Imports System.Drawing.Imaging
Imports System.Globalization
Imports System.IO
Public Class ImageConversion
Implements IDisposable
Private IsDisposed As Boolean = False
Private CurrentFile As String
Private CurrentBitmap As Image
Private Enum FilterType
OpenFile
SaveFile
End Enum
Public Function OpenFile() As (OpenedBitmap As Image, FileName As String)
Using OFD As OpenFileDialog = New OpenFileDialog()
With OFD
.CheckFileExists = True
.CheckPathExists = True
.RestoreDirectory = True
.Title = "Open Image File"
.Filter = GetFileFilters(FilterType.OpenFile)
.FilterIndex = 4
.FileName = ""
End With
If OFD.ShowDialog() = DialogResult.Cancel Then Return Nothing
CurrentFile = OFD.FileName
If CurrentBitmap IsNot Nothing Then CurrentBitmap.Dispose()
CurrentBitmap = CType(Image.FromFile(CurrentFile).Clone(), Bitmap)
End Using
Return (CurrentBitmap, CurrentFile)
End Function
Public Function SaveFileFormat() As (FileName As String, ErrorMessage As String)
Dim NewFileName As String = Path.GetFileNameWithoutExtension(CurrentFile)
Dim ErrorMessage As String = String.Empty
Using SFD As SaveFileDialog = New SaveFileDialog()
With SFD
.AddExtension = True
.ValidateNames = True
.CheckPathExists = True
.RestoreDirectory = True
.Title = "Save Image File"
.Filter = GetFileFilters(FilterType.SaveFile)
.FileName = NewFileName
End With
If SFD.ShowDialog() = DialogResult.Cancel Then Return (String.Empty, String.Empty)
Try
NewFileName = SFD.FileName
Dim imgFormat As ImageFormat = GetImageFormat(NewFileName)
CurrentBitmap.Save(NewFileName, imgFormat)
Catch ex As IOException
NewFileName = String.Empty
End Try
End Using
Return (NewFileName, ErrorMessage)
End Function
Private Function GetImageFormat(FileName As String) As ImageFormat
Dim fileType As String = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Path.GetExtension(FileName).Remove(0, 1).ToLower())
If fileType = "Tif" Then fileType = "Tiff"
If fileType = "Jpg" Then fileType = "Jpeg"
Dim imgFormat As ImageFormat = New ImageFormat(New Guid())
Return DirectCast(imgFormat.GetType().GetProperty(fileType).GetValue(imgFormat), ImageFormat)
End Function
Private Function GetFileFilters(Filter As FilterType) As String
Dim Filters As String() = {
"BMP Files|*.bmp", "|GIF Files|*.gif", "|JPEG Files|*.jpg",
"|PNG Files|*.png", "|TIFF Files|*.tif", "|Enhanced Windows MetaFile|*.emf",
"|Exchangeable Image File|*.exif", "|Icons|*.ico", "|Windows MetaFile|*.wmf"
}
Select Case Filter
Case FilterType.OpenFile
Return String.Join("", Filters)
Case FilterType.SaveFile
Return String.Join("", Filters.Take(5))
Case Else
Return String.Empty
End Select
End Function
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Sub Dispose(disposing As Boolean)
If IsDisposed Then Return
If disposing Then
If CurrentBitmap IsNot Nothing Then
CurrentBitmap.Dispose()
End If
End If
IsDisposed = True
End Sub
End Class
I've modified your original class to add some missing features:
- The selected File Format returned by the
SaveFileDialog()
- The selection of Filters in
SaveFileDialog()
is reduced to the formats that Image.Save() actually supports
IDisposable
support, used to dispose of the Bitmap that is currently loaded- Some others details you can find in the notes and in the class code
This is now a stand-alone class that can be used in other contexts (there's no reference to specific controls or methods:
Sample usage:
Initialize the ImageConversion
class (in a Form's constructor or where you think is appropriate):
Public Partial Class Form1
Public imgConversion As ImageConversion = New ImageConversion()
'(...)
End Class
Your handlers can be modified like this:
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose()
Dim NewBitmapFile = imgConversion.OpenFile()
If NewBitmapFile.OpenedBitmap IsNot Nothing Then
PictureBox1.Image = NewBitmapFile.OpenedBitmap
Me.Text = NewBitmapFile.FileName
End If
End Sub
Private Sub BitmapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BitmapToolStripMenuItem.Click
Dim SavedFile = imgConversion.SaveFileFormat()
If SavedFile.ErrorMessage <> String.Empty Then
MessageBox.Show("Failed to save image to Bitmap.", "Error" & SavedFile.ErrorMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf SavedFile.FileName <> String.Empty Then
MessageBox.Show("Image File saved to: " + SavedFile.FileName, "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Dispose of the ImageConversion
class when the Form is closed, this will free the last Image object used:
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
imgConversion.Dispose()
End Sub
To note:
Since you are using a method that returns a
Bitmap
object and this object is then assigned to aPictureBox.Image
property, you need to dispose of the previous Image, if any, before assigning a new one. Otherwise this lost object will linger on more that it's desired and it this will hamper you app.The Filters, in the
SaveFileDialog
, have beed reduced to theImageFormats
that are actually supported by GDI+ when creating a new Bitmap.Icon
,WMF
,EMF
andExif
are not supported and the resulting Image will be aPNG
file format, the default GDI+ format.
The first three can be created with other means, but this is a broad matter and can't be addressed here.
A couple of methods return a ValueTuple, in the form of
ValueTuple(Of T1, T2)
.
I'm not sure your VB.Net version supports this return type and form.
If not, the method can be modified to returnByRef
results or a specialized public object (a sub-class ofImageConversion
) that holds the required informations (probably preferable).
Public Function OpenFile() As (OpenedBitmap As Image, FileName As String)
'(...)
Dim result = OpenFile()
Dim img As Bitmap = result.OpenedBitmap
Dim fName As String = result.FileName
The ImageConversion
class:
Imports System.Drawing.Imaging
Imports System.Globalization
Imports System.IO
Public Class ImageConversion
Implements IDisposable
Private IsDisposed As Boolean = False
Private CurrentFile As String
Private CurrentBitmap As Image
Private Enum FilterType
OpenFile
SaveFile
End Enum
Public Function OpenFile() As (OpenedBitmap As Image, FileName As String)
Using OFD As OpenFileDialog = New OpenFileDialog()
With OFD
.CheckFileExists = True
.CheckPathExists = True
.RestoreDirectory = True
.Title = "Open Image File"
.Filter = GetFileFilters(FilterType.OpenFile)
.FilterIndex = 4
.FileName = ""
End With
If OFD.ShowDialog() = DialogResult.Cancel Then Return Nothing
CurrentFile = OFD.FileName
If CurrentBitmap IsNot Nothing Then CurrentBitmap.Dispose()
CurrentBitmap = CType(Image.FromFile(CurrentFile).Clone(), Bitmap)
End Using
Return (CurrentBitmap, CurrentFile)
End Function
Public Function SaveFileFormat() As (FileName As String, ErrorMessage As String)
Dim NewFileName As String = Path.GetFileNameWithoutExtension(CurrentFile)
Dim ErrorMessage As String = String.Empty
Using SFD As SaveFileDialog = New SaveFileDialog()
With SFD
.AddExtension = True
.ValidateNames = True
.CheckPathExists = True
.RestoreDirectory = True
.Title = "Save Image File"
.Filter = GetFileFilters(FilterType.SaveFile)
.FileName = NewFileName
End With
If SFD.ShowDialog() = DialogResult.Cancel Then Return (String.Empty, String.Empty)
Try
NewFileName = SFD.FileName
Dim imgFormat As ImageFormat = GetImageFormat(NewFileName)
CurrentBitmap.Save(NewFileName, imgFormat)
Catch ex As IOException
NewFileName = String.Empty
End Try
End Using
Return (NewFileName, ErrorMessage)
End Function
Private Function GetImageFormat(FileName As String) As ImageFormat
Dim fileType As String = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Path.GetExtension(FileName).Remove(0, 1).ToLower())
If fileType = "Tif" Then fileType = "Tiff"
If fileType = "Jpg" Then fileType = "Jpeg"
Dim imgFormat As ImageFormat = New ImageFormat(New Guid())
Return DirectCast(imgFormat.GetType().GetProperty(fileType).GetValue(imgFormat), ImageFormat)
End Function
Private Function GetFileFilters(Filter As FilterType) As String
Dim Filters As String() = {
"BMP Files|*.bmp", "|GIF Files|*.gif", "|JPEG Files|*.jpg",
"|PNG Files|*.png", "|TIFF Files|*.tif", "|Enhanced Windows MetaFile|*.emf",
"|Exchangeable Image File|*.exif", "|Icons|*.ico", "|Windows MetaFile|*.wmf"
}
Select Case Filter
Case FilterType.OpenFile
Return String.Join("", Filters)
Case FilterType.SaveFile
Return String.Join("", Filters.Take(5))
Case Else
Return String.Empty
End Select
End Function
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Sub Dispose(disposing As Boolean)
If IsDisposed Then Return
If disposing Then
If CurrentBitmap IsNot Nothing Then
CurrentBitmap.Dispose()
End If
End If
IsDisposed = True
End Sub
End Class
edited Nov 27 '18 at 0:58
answered Nov 24 '18 at 23:25
JimiJimi
7,40241833
7,40241833
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455103%2fimage-conversion-in-windows-forms%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You should make use of the
Image Converter
Class– preciousbetine
Nov 24 '18 at 15:11
1
@Jimi : Actually he isn't just changing the extension. His code loads the selected image into memory (which always uses the same format: an uncompressed memory bitmap) and then saves it, specifying the format to use. GDI+ takes care of the rest.
– Visual Vincent
Nov 24 '18 at 19:16
1
@preciousbetine : The
ImageConverter
class is used to convert anImage
to/from different data types. To convert an image into a different format you've got to use theBitmap.Save(String, ImageFormat)
overload.– Visual Vincent
Nov 24 '18 at 19:21
@VisualVincent yeah that works.
– preciousbetine
Nov 24 '18 at 19:24
@TravisRoberts : You set the
Filter
property for theOpenFile
dialog but you never seem to do the same for theSaveFile
dialog (unless you do so through the designer). Is this the case?– Visual Vincent
Nov 24 '18 at 19:29