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.

    '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:
    '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
    ' 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

    Dim c As Canvas
    Dim r1 As Rect
    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
    'You can make the background color something else.
        'I didn't need it as a parameter.
    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 :)
  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