Private Function GetScaledImage(ImgMs As System.IO.Stream, MaxWidth As Integer, MaxHeight As Integer) As resizedimage Try ImgMs.Seek(0, IO.SeekOrigin.Begin) Dim tempi As New BitmapImage tempi.SetSource(ImgMs) Dim cx As Double = CDbl(MaxWidth) Dim cy As Double = tempi.PixelHeight * (cx / tempi.PixelWidth) If cy > CDbl(MaxHeight) Then cy = CDbl(MaxHeight) cx = tempi.PixelWidth * (cy / tempi.PixelHeight) End If Dim img As New Image img.Source = tempi Dim wb1 As New WriteableBitmap(CInt(cx), CInt(cy)) Dim st As New ScaleTransform st.ScaleX = cx / tempi.PixelWidth st.ScaleY = cy / tempi.PixelHeight wb1.Render(img, st) wb1.Invalidate() Dim wb2 As New WriteableBitmap(CInt(cx), CInt(cy)) For i As Integer = 0 To wb2.Pixels.Length - 1 wb2.Pixels(i) = wb1.Pixels(i) Next wb2.Invalidate() Dim jj As New resizedimage With {.x = wb2.PixelWidth, .y = wb2.PixelHeight, .mebytes = wb2.ToByteArray} wb1 = Nothing wb2 = Nothing img = Nothing tempi = Nothing GC.Collect() Return jj Catch ex As Exception Return Nothing End Try
Private Function GetScaledImageNoResize(tempi As BitmapImage) As resizedimage Try Dim wb1 As New WriteableBitmap(tempi) wb1.Invalidate() Dim wb2 As New WriteableBitmap(wb1.PixelWidth, wb1.PixelHeight) For i As Integer = 0 To wb2.Pixels.Length - 1 wb2.Pixels(i) = wb1.Pixels(i) Next wb2.Invalidate() Dim jj As New resizedimage With {.x = wb2.PixelWidth, .y = wb2.PixelHeight, .mebytes = wb2.ToByteArray} wb1 = Nothing wb2 = Nothing tempi = Nothing GC.Collect() Return jj Catch ex As Exception Return Nothing End Try End Function
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)