ASP.Net GDI+
[40 mn de lecture - paru le 5/8/2004 1:54:12 PM - Public : Confirmé]
|
   
|
Auteur
4. How Resize
4.1. Resize
The resizing
of an image is done in two stages in this class. Initially, a new image is created with the required size, on the basis of image of origin. Then, the original image is removed and replaced by the new image
.
///<summary>
/// resize image
/// </summary>
/// <param name="width">
///width
/// </param>
/// <param name="height">
/// height
/// </param>
public void Resize(int width, int height)
{
Size s = new Size(width, height);
Resize(s);
}
/// <summary>
/// resize picture
/// </summary>
/// <param name="newSize">
/// new size
/// </param>
public void Resize(Size newSize)
{
if(newSize.Width <= 0 || newSize.Height <= 0)
throw new ArgumentOutOfRangeException("Spécifie une taille dans l'image.");
Bitmap tempImage = new Bitmap(monImage,newSize);
monImage.Dispose();
monImage = tempImage;
}
The effective redimensioning of the image is carried out during the creation of the new image, since one of the options during the creation of an image is to base it on an existing image. Also note that the image of origin calls the Dispose method so that it releases the resources become useless. There are two versions of the Resize function, which leaves you the choice of the types of parameter.
4.2. Crops
To cut down an image is slightly more complicated than redimensioning. It is not a method of GDI+.
We will create methods trimming of an image reduces the size of the image while cutting part of this one. It redimensionne not contents of the image of origin.
We will set up two poured function Rogner, each one accepting of the different parameters. The first version takes a width and a height. These dimensions are then centered on the image and a rectangle of trimming is calculated. This last is then transmitted to the second version of the Crop function to carry out the trimming of the image.
/// <summary>
/// crop image
/// </summary>
/// <param name="cropRect">
/// size of image
/// </param>
public void Rogner(Rectangle cropRect)
{
//
to create the new image in which to cut down the image
Bitmap tempImage = new Bitmap( cropRect.Width,
cropRect.Height, monImage.PixelFormat);
Graphics g = Graphics.FromImage(tempImage);
g.DrawImage(monImage,
0,0,
cropRect,
GraphicsUnit.Pixel);
g.Dispose();
monImage.Dispose();
monImage = tempImage;
}
/// <summary>
/// crop image
/// </summary>
/// <param name="width">
/// with pixel
/// </param>
/// <param name="height">
/// height pixel
/// </param>
public void Rogner(int cropWidth,int cropHeight)
{
// to cut down only if specified dimensions are lower
// with those of the image of origin
if(cropWidth > monImage.Width)
cropWidth = monImage.Width;
if(cropHeight > monImage.Height)
cropHeight = monImage.Height;
//
to calculate the rectangle of rognagee
int xStart = (monImage.Width - cropWidth);
if(xStart != 0)
xStart /= 2;
int yStart = (monImage.Height - cropHeight);
if(yStart != 0)
yStart /= 2;
Rectangle cropRect = new Rectangle(
xStart, yStart,
cropWidth, cropHeight);
//to make a crop
Rogner(cropRect);
}
The second methods To cut down to overload takes a rectangle like parameter. That makes it possible to have a complete control on the zone to cut down image.
To carry out trimming, a new image is created using the dimensions specified in cropRect and the format pixel specified in the image of origin (the format pixel refers to the depth of colors, such as 256 colors, color 24 bits, color 32 bits, etc.)
Then, a graphic context is created on the new image. This last is necessary to carry out any operation of drawing in NET.
You defer to class NET System.Drawing.Graphics for more information on the graphic contexts. Like the graphic context was created starting from the image, all the drawing will be carried out directly on the image.
Once the graphic context created, the image of origin is then drawn on the new image, or at least only part of the original one. One of the parameters which the DrawImage function can accept is a rectangle which indicates the zone to be preserved. Thus, at the place which the whole image is drawn, only the part being in the co-ordinates of the rectangle is preserved.
Once the new image created, the graphic context is removed, and the image of origin is then replaced by the cut down image.
4.3. Example of image redimensionner
For our example put in your page ASPX a Control Button, double click above.
You arrive in the code behind, in the onClick part to insert:
private void redimensionnerButton_Click(object sender, System.EventArgs e)
{
String path = ImageButton1.ImageUrl.Replace("file:///","").ToString();// path from image
traitementImage monImage = new traitementImage();// instanciation of classe traitementImage
monImage.chargementImage(path);// loading image
monImage.Resize(100,100);// resize image with 100
String nouveaNom = Server.MapPath(Request.ApplicationPath) + "/miniaturePhoto.JPG";// path and new name
monImage.SaveImageAs(nouveaNom);// save as
}
As you can note it the image is diforme, to carry out best the redimension you will have to juggle between the method Rogner and Resize.
4.4. Creating thumbnails
And yes Net with envisaged a method to generate miniatures (label of image JPG, GIF, tiff...): GetThumbnailImage
public void miniature()
{
Image.GetThumbnailImageAbort tnCallBack = new Image.GetThumbnailImageAbort(tnCallbackMethod);
// Get the thumbnail image
Image thumbNailImage = monImage.GetThumbnailImage (100, 100, tnCallBack, IntPtr.Zero);
// create graphic objet
Graphics g = Graphics.FromImage(monImage);
g.Clear(System.Drawing.Color.White);
// creating thumbnails
g.DrawImage(thumbNailImage, 40, 20);
//dispose graphic ressource
g.Dispose();
monImage.Dispose();
monImage = thumbNailImage;
}
//
must be to call but not used
public bool tnCallbackMethod()
{
return false;
}
|