ImageMod

Discussion in 'Old Libraries Archive' started by kanaida, Sep 10, 2013.

  1. kanaida

    kanaida Active Member Licensed User

    Here's a new little module to do stuff with graphics. Any additions welcome.
    So far it was made to make the ImageDownloader more convenient.

    Code:
    'ImageMod Code Module
    'By: Kanaida
    'Created 9/10/2013
    'Version: 1.0

    'Class module
    Sub Class_Globals
      
    End Sub

    'Initializes the object. You can add parameters to this method if needed.
    Public Sub Initialize

    End Sub

    ' Copy the image from pic_src into pic_dst so it fits and
    ' has the same aspect ratio as the original picture.
    Public Sub FitBitmapToView(pic_src As Bitmap, pic_dst As ViewAs Bitmap
    'Original Source: http://www.vb-helper.com/howto_fit_picture_to_box.html
    'Ported by: Kanaida
    'Created: 9/10/2013
    'Description: Original made to work with the ImageDownloader Service to
    'fit bitmaps To target ImageViews

    Dim aspect_src As Float
    Dim wid As Float
    Dim hgt As Float

        
    ' Get the original picture's aspect ratio.
        aspect_src = pic_src.Width / pic_src.Height

        
    ' Get the size available.
        wid = pic_dst.Width
        hgt = pic_dst.Height

        
    ' Adjust the wid/hgt ratio to match aspect_src.
        If wid / hgt > aspect_src Then
            
    ' The area is too short and wide.
            ' Make it narrower.
            wid = aspect_src * hgt
        
    Else
            
    ' The area is too tall and thin.
            ' Make it shorter.
            hgt = wid / aspect_src
        
    End If

        
    ' Center the image at the correct size.
      
        
    Dim b2 As Bitmap
        b2.InitializeMutable(pic_dst.Width,pic_dst.Height)

        
    Dim c As Canvas
        c.Initialize2(b2)
      
        
    Dim r1 As Rect
        r1.Initialize(
    0,0,pic_src.Width,pic_src.Height)
      
        
    Dim Left As Float =(pic_dst.Width - wid) / 2
        
    Dim Top As Float = (pic_dst.Height - hgt) / 2
      
        
    Dim Right As Float = Left+wid
        
    Dim Bottom As Float = Top+hgt
      
        
    Dim r2 As Rect
        r2.Initialize(Left,Top,Right,Bottom)
      
        
    'You can make the background color something else.
        'I didn't need it as a parameter.
        c.DrawColor(Colors.White)
        c.DrawBitmap(pic_src,r1,r2)
      
        
    Return b2
    End Sub
     
    Last edited: Sep 11, 2013
    Erel likes this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    Thank you for sharing this.

    Small tip: writing c = null is not needed. c is a local variable. The reference will be removed automatically when the sub finishes.
     
  3. kanaida

    kanaida Active Member Licensed User

    Thanks. Wasn't sure what happened when it fell out of scope, used to calling .dispose() a lot in .net to be nice to the trash can :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice