Metodo di estensione per clonare una Bitmap ridimensionandola
Scritto da
Massimo Bonanni il
giovedì 10 febbraio 2011
•
Linguaggio:
• Livello: 100
Questo semplice metodo di estensione accetta un oggetto di
classe System.Drawing.Bitmap e ne restituisce una
copia ridimensionata in base a un fattore x e un fattore y:
VB.NET
<Extension()> _
Public Function CloneResize(ByVal image As System.Drawing.Bitmap, _
ByVal zoomFactorWidth As Double, _
ByVal zoomFactorHeight As Double) As System.Drawing.Bitmap
If image Is Nothing Then Throw New ArgumentNullException("image")
Dim width = image.Width
Dim height = image.Height
Dim newImage = New System.Drawing.Bitmap(image, _
Convert.ToInt32(width * zoomFactorWidth), _
Convert.ToInt32(height * zoomFactorHeight))
Return newImage
End Function
C#
public static System.Drawing.Bitmap CloneResize(this System.Drawing.Bitmap image,
double zoomFactorWidth,
double zoomFactorHeight)
{
if (image == null) throw new ArgumentNullException("image");
Int32 width = image.Width;
Int32 height = image.Height;
System.Drawing.Bitmap newImage = new System.Drawing.Bitmap(image,
Convert.ToInt32(width * zoomFactorWidth),
Convert.ToInt32(height * zoomFactorHeight));
return newImage;
}
Tags: extension methods