Click to See Complete Forum and Search --> : Image Lists


PaulJayKnight
November 12th, 2008, 11:22 AM
When I put an image from a file in a PictureBox with SizeMode set to StretchImage I get an effect
that I like. Is there any way to format images from a file before putting them into an ImageList to
get the same effect? these images are being used in objects that don't have a SizeMode.

Cimperiali
November 14th, 2008, 09:10 AM
Yes. For example:
in a form, with a picture, a button and an OpenFileDialog
(the picture sizemode is center, the picture is anchored to all sides)
(so if you load a picture, and trhen resize the form, you will see
how the picture chancge in size and width, and the image is resized
accordingli, but not by the pictureBox, by the code)

Public Class Form1
Dim ResizeImg As New ResizeImage
'to do subsequent resiziong on the
'original one so not to lose too much details:
Dim originalImg As Image

Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
With OFDGetPicture

.CheckFileExists = True
.Filter = "all graphics|*.bmp;*.jpg;*.jpeg;*.gif|bmp|*.bmp|jpg|*.jpg;*.jpeg|gif|*.gif|all files|*.*"
.Multiselect = False
.ShowHelp = False
.ShowReadOnly = False

If .ShowDialog = Windows.Forms.DialogResult.OK Then

picFixed.Image = ResizeImg.ResizeImage(.FileName, picFixed.Height, picFixed.Width, originalImg)

End If

End With

End Sub

Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
If Me.WindowState <> FormWindowState.Minimized Then
If picFixed.Image IsNot Nothing Then
'here: use originalImg, not to lose too much details!)
picFixed.Image = ResizeImg.ResizeImage(originalImg, picFixed.Height, picFixed.Width)
End If
End If
End Sub
End Class


in a class:

Imports System.IO
Imports System.Drawing

Public Class ResizeImage




Public Function ResizeImage(ByVal OriginalImage As Image _
, ByVal NewHeight As Integer _
, ByVal NewWidth As Integer _
) As Image

'the easiest: if you know witdth and height you want
'and you have the image

Return OriginalImage.GetThumbnailImage(NewWidth, NewHeight, Nothing, Nothing)



End Function

Public Function ResizeImage(ByVal filePathNameOfOriginalImage As String _
, ByVal NewHeight As Integer _
, ByVal NewWidth As Integer _
, ByRef OriginalImage As Image) As Image

'overload 2: if you know witdth and height you want, and you need
'to get image from a file. You return also the original image not to lose
'too details if resize are asked again: caller should subsequently call the first overload
'always passing this OriginalImage parm
OriginalImage = GetImageFromFile(filePathNameOfOriginalImage)
Return ResizeImage(OriginalImage _
, NewHeight, NewWidth)



End Function

Private Function GetImageFromFile(ByVal filePathName As String) As Image
'get an image from a file

Dim RetVal As Image = Nothing
If File.Exists(filePathName) Then

RetVal = System.Drawing.Image.FromFile(filePathName)
End If
Return RetVal
End Function


End Class

PaulJayKnight
November 16th, 2008, 01:13 AM
Thank you that worked

Cimperiali
November 18th, 2008, 08:03 AM
By the way, if you play with jpg/jpeg, you might experience a random data loss due to the fact that if a thumbnail is contained inside an image, the getThumbnail will (if it can) get the inside thumb istead of the main Image.
In case, you need to get rid of the internal thumb. A way to do it is to convert the loaded image in a Bmp before.
The zip here contain a sample where that job is done in memory.
Have a look: