![]()
Veronica, che niente niente vieni da asp?![]()
guardati questo modulo e vedi cosa sbagli, vedrai che sarà un ottimo esercizio...![]()
![]()
(fai tasto dx su progetto-> aggiungi-> nuovo elemento-> modulo)
e si usa così :codice:Imports System.Drawing.Drawing2D Imports System.IO Imports System.Drawing.Imaging Module ImageExtender Sub New() End Sub <System.Runtime.CompilerServices.Extension()> _ Public Function ResizeImage(Image As Image, Width As Integer, Height As Integer) As Image Dim widthImage As Integer = Image.Width Dim heightImage As Integer = Image.Height If widthImage > Width OrElse heightImage > Height Then Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX) Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX) Dim ratio As Single = 0 If widthImage < heightImage Then ratio = CSng(widthImage) / CSng(heightImage) widthImage = Width heightImage = Convert.ToInt32(Math.Round(CSng(widthImage) / ratio)) Else ratio = CSng(heightImage) / CSng(widthImage) heightImage = Height widthImage = Convert.ToInt32(Math.Round(CSng(heightImage) / ratio)) End If Return Image.GetThumbnailImage(widthImage, heightImage, Nothing, IntPtr.Zero) End If Return Image End Function <System.Runtime.CompilerServices.Extension()> _ Public Function CropImage(Image As Image, Width As Integer, Height As Integer, X As Integer, Y As Integer, Imageformat As ImageFormat, _ PixelFormat As PixelFormat) As Image Dim result As Image Dim ms As MemoryStream = Nothing Try If Image.Height < Height Then Height = Image.Height End If If Image.Width < Width Then Width = Image.Width End If Dim ImageTemp As New Bitmap(Width, Height, PixelFormat) ImageTemp.SetResolution(72, 72) Dim graphic As Graphics = Graphics.FromImage(ImageTemp) graphic.SmoothingMode = SmoothingMode.AntiAlias graphic.InterpolationMode = InterpolationMode.HighQualityBicubic graphic.PixelOffsetMode = PixelOffsetMode.HighQuality graphic.DrawImage(Image, New Rectangle(0, 0, Width, Height), X, Y, Width, Height, _ GraphicsUnit.Pixel) ms = New MemoryStream() ImageTemp.Save(ms, Imageformat) Image.Dispose() ImageTemp.Dispose() graphic.Dispose() result = Image.FromStream(ms) Return result Catch er As Exception Throw New Exception(er.Message) End Try End Function <System.Runtime.CompilerServices.Extension()> _ Public Function CropImage(Image As Image, Width As Integer, Height As Integer, X As Integer, Y As Integer) As Image Return Image.CropImage(Width, Height, X, Y, ImageFormat.Bmp, PixelFormat.Format24bppRgb) End Function <System.Runtime.CompilerServices.Extension()> _ Public Function ResizeAndCropCenter(image As Image, width As Integer, height As Integer) As Image Dim result As Image = image.ResizeImage(width, height) Dim x As Integer = If(result.Width > result.Height, (result.Width - width) / 2, 0) Dim y As Integer = If(result.Width < result.Height, (result.Height - height) / 2, 0) Return result.CropImage(width, height, x, y) End Function End Module
codice:Image.FromFile("C:\cartella\immagine.png").ResizeAndCropCenter(500, 376).Save("immagineRidimensionata.jpg") 'che equivale a Dim immagineOriginale = Image.FromFile("C:\cartella\immagine.png") Dim NuovaImmagineRidimensionata = immagineOriginale.ResizeAndCropCenter(500, 376) NuovaImmagineRidimensionata.Save("immagineRidimensionata.jpg")![]()

Rispondi quotando