Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15050

Changing an Image Size

$
0
0
Firstly I am still using the 2013 version of vb.net

I am writing an image processing routine and after 50 or so images it kept running out of memory because Bitmaps are memory hogs and hang around apparently

Anyway this works and may be useful for others

Code:

Public Function ImageSizeChangeLBF(imgSource As Image, intWidth As Integer, intHeight As Integer) As Image
  ' Written 5th March 2014

  Dim memStream As New MemoryStream          ' MUST go outside the Using Statements

  ' Make a bitmap for the Destination
  Using bmpDestination As New Bitmap(intWidth, intHeight)

    ' Make a Graphics object for the Destination Bitmap.
    Using gphDesination As Graphics = Graphics.FromImage(bmpDestination)

      ' Copy the source image into the Destination bitmap with different Dimensions
      gphDesination.DrawImage(imgSource, 0, 0, intWidth + 1, intHeight + 1)

      ' Copy to a Memory Stream (CANNOT JUST SET IMAGE = BITMAP AS THIS IS JUST MEMORY AND IT IS RESET BY "END USING")
      bmpDestination.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp)

    End Using
  End Using

  '  Return
  ImageSizeChangeLBF = Image.FromStream(memStream)

End Function

So just do this

picBox.Image = ImageSizeChangeLBF(picBox.Image, intWidth, intHeight )

Hope this helps somebody
Bob

Ps You need: Imports System.Drawing
.

Viewing all articles
Browse latest Browse all 15050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>