CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Image Lists

  1. #1
    Join Date
    Jul 2008
    Posts
    52

    Image Lists

    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.

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Image Lists

    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)
    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:
    Code:
    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
    Last edited by Cimperiali; November 14th, 2008 at 10:14 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Jul 2008
    Posts
    52

    Smile Re: Image Lists

    Thank you that worked

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Image Lists - by the way...

    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:
    Attached Files Attached Files
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured