Sauver une portion d'image
Bonsoir,
Est-il possible de sauver une portion d'image sans passer par une PictureBox?
J'essaye de m'expliquer avec un exemple:
- Une image 6000x4000px
- Un rectangle virtuel avec x=300, y=200, width=600 et Height=400
- On sauve l'image => Nouvelle image 600x400px, demarrant aux coordonnees (300,200) de l'image originale
Je pensais utiliser un MemoryStream et creer une fonction, mais je ne suis pas sur d'etre sur la bonne voie. Un peu comme ci-dessous, meme s'il y a des erreurs a corriger.
Est-ce-que, au moins, c'est la bonne facon de proceder? Ou avez-vous une meilleure facon de faire?
Code:
1 2 3 4 5
|
Dim newImage As Image
Dim memStream As MemoryStream = New MemoryStream
newImage = ResizeImage(FileName, memStream, x, y, Width, Height)
newImage.Save(memStream, ImageFormat.Jpeg) |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Public Function ResizeImage(ByVal ImagePath As String, ByVal SavePath As Stream, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer) As Image
' http://www.vbforums.com/showthread.php?550637-Reduce-file-size-of-images-(bmp-s-jpg-s-)
Dim bm As New Bitmap(ImagePath)
' Declaration de variables x, y, w, h
Dim thumb As New Bitmap(x, y, w, h)
Using g As Graphics = Graphics.FromImage(thumb)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(bm, New Rectangle(0, 0, w, h), New Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel)
thumb.Save(SavePath, System.Drawing.Imaging.ImageFormat.Jpeg)
End Using
Return thumb
End Function |
Merci,
A+
JLuc01