Hallo,
ich habe das folgende Problem:
Ich habe eine Funktion gebaut die Bilder beschneiden kann. Das funktioniert auch super. Das Problem ist, dass bei sehr vielen Bildern der Speicher immer weiter voll läuft.
Ich vermute, dass es daran liegt, das ich die Objekte nicht richtig schliesse.
Mit den Return fixBmp wird die Function verlassen. Ich vermute aber, dass dabei das Object offen bleibt und den Fehler verursacht.
Public Function Beschneiden(ByVal Bmp As Bitmap) As Bitmap
Dim CropRight As Integer, CropLeft As Integer, CropTop As Integer, _
CropBottom As Integer
'von Links
For CropLeft = 0 To Bmp.Width - 1
If beschneiden_check_row(Bmp, CropLeft) = False Then CropLeft -= 1 _
: Exit For
Next
If CropLeft = -1 Then CropLeft = 0
'von Rechts
For CropRight = Bmp.Width - 1 To 0 Step -1
If beschneiden_check_row(Bmp, CropRight) = False Then CropRight += _
1 : Exit For
Next
'von Oben
For CropTop = 0 To Bmp.Height - 1
If beschneiden_check_col(Bmp, CropTop) = False Then CropTop -= 1 : _
Exit For
Next
If CropTop = -1 Then CropTop = 0
'von Unten
For CropBottom = Bmp.Height - 1 To 1 Step -1
If beschneiden_check_col(Bmp, CropBottom) = False Then CropBottom _
+= 1 : Exit For
Next
If CropBottom = Bmp.Height And CropTop = 0 And CropLeft = 0 And _
CropRight = Bmp.Width Or CropBottom - CropTop < 0 Or CropRight - _
CropLeft < 0 Then Return Bmp
Dim NewWidth As Integer = CropRight - CropLeft
Dim NewHeight As Integer = CropBottom - CropTop
Dim fixBmp As Bitmap = New Bitmap(NewWidth, NewHeight, _
PixelFormat.Format32bppArgb)
Dim g As Drawing.Graphics = Graphics.FromImage(fixBmp)
g.Clear(Color.White)
g.SmoothingMode = SmoothingMode.HighQuality
g.CompositingQuality = CompositingQuality.HighQuality
g.InterpolationMode = InterpolationMode.High
g.DrawImage(Bmp, New RectangleF(0, 0, NewWidth, NewHeight), New _
RectangleF(CropLeft, CropTop, NewWidth, NewHeight), _
GraphicsUnit.Pixel)
Return fixBmp
End Function Wie kann ich das Problem lösen?
Joachim
Joachim |