ASP.Net GDI+
[40 mn de lecture - paru le 5/8/2004 1:54:12 PM - Public : Confirmé]
|
   
|
Auteur
3. Loading, Recording, Visualization
3.1. Creation of the Class traitementImage
In your Visual Project you must add a new Class which you entitle "traitementImage"
Place the following nameSpaces in the heading :
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using imagetest = System.Drawing; // create
alias to avoid all ambiguity
And to add you these data members of the class into private so that they are not accessible:
private Image monImage; //
// the image in progress
private String imageNom = ""; //name of image
private ImageFormat imageFormat; // format of image
3.2. Loading of the image
For the loading of the image one should not forgets to call the Dispose method of the images to guarantee a release of the resources, if the class would contain already an image.
/// <summary>
/// dispose ressource
/// </summary>
public void Dispose()
{
if(monImage != null)
monImage.Dispose();
}
To load the image one creates an Image object of the source file by using the methods FromFile or FromHbitmap or FromStream.
/// <summary>
/// load image
/// </summary>
/// <param name="cheminImage">
/// path if image
/// </param>
public void chargementImage(string cheminImage)
{
if(monImage != null) monImage.Dispose();
monImage = Image.FromFile(cheminImage);
imageNom = cheminImage;
imageFormat = monImage.RawFormat;
}
3.3. Save Image
The class Imaging itself carries out large work of recording of the image.
Although the garbage-collector can itself release the resources, it is not guaranteed that it does it and that can result in appreciably reducing the resources available for the system itself. The file name which is stored can be used as reference at the time of the next operations of recording.
The Save method sub-contracts with the method of the images by using the file name of origin previously stored.
The SaveAs method is declined in two overloads.
/// <summary>
/// save image
/// </summary>
public void SaveImage()
{
monImage.Save(imageNom,imageFormat);
}
/// <summary>
/// save as
/// </summary>
/// <param name="nouveauNom">
/// name from image
/// </param>
public void SaveImageAs(String nouveauNom)
{
monImage.Save(nouveauNom,imageFormat);
}
/// <summary>
/// save as image with a new name
/// </summary>
/// <param name="nouveauNom">
/// new name of image
/// </param>
/// <param name="format">
/// choice new Format (gif, jpg, bmp, tiff, etc.)
/// </param>
public void SaveImageAs(String nouveauNom,ImageFormat nouveauFormat)
{
monImage.Save(nouveauNom,nouveauFormat);
}
3.4. Property of the image
We will establish several methods allowing us to have the maximum of information on the photograph which we want to treat: the taille(largor, height), resolution, format...
/// <summary>
/// return size of image
/// </summary>
public int Width
{
get
{
return monImage.Width;
}
}
/// <summary>
/// retourn height if image
/// </summary>
public int Height
{
get
{
return monImage.Height;
}
}
/// <summary>
/// return size of image
/// </summary>
public Size Size
{
get
{
return monImage.Size;
}
}
/// <summary>
/// return Horizontal resolution
/// </summary>
public float HorizontalResolution
{
get
{
return monImage.HorizontalResolution;
}
}
/// <summary>
/// return Vertical vertical
/// </summary>
public float VerticalResolution
{
get
{
return monImage.VerticalResolution;
}
}
/// <summary>
/// retunr format from image
/// </summary>
public string RawFormat
{
get
{
return monImage.RawFormat.ToString();
}
}
/// <summary>
/// retunr image type
/// </summary>
public string PixelFormat
{
get
{
return monImage.PixelFormat.ToString();
}
}
3.5. Weight of the image
For the poid I small appartée for you given an easy way makes in order to know the poid of a photograph (it is the same thing for any file)
Do not forget to import using System.IO; It is the namespace which gére files.
/// <summary>
/// return heigth
/// </summary>
/// <param name="cheminImage">
/// path of image
/// </param>
public string poidImage(string cheminImage)
{
FileInfo monImagePoid = new FileInfo(cheminImage);// property nameSpace IO
if(monImagePoid.Length > 1000000)
{
return (monImagePoid.Length /1000000) + " Mo";// return lenght in Mo
}
else
{
if(monImagePoid.Length > 1000)
{
return (monImagePoid.Length /1000) + " Ko";// return lenght in ko
}
else
{
return monImagePoid.Length + " octets";// return lenght in octect
}
}
}
3.6. Intanciation of the Class traitementImage
We will establish several methods allowing us to have the maximum of information on the photograph which we want to treat: the taille(largor, height), resolution, format...
in the aspx page place two control: a Label and ImageButton which you will nomerez respectively infoLabel and ImageButton1:
< asp:Label id="infoLabel "runat=server/>
< asp:ImageButton id="ImageButton1 "runat=server ImageUrl="fille:///E:\photo.JPG"/>
in the Behind code:
private void Page_Load(object sender, System.EventArgs e)
{
String path = ImageButton1.ImageUrl.Replace("file:///","").ToString();
traitementImage monImage = new traitementImage();// classe traitementImage
monImage.chargementImage(path);// load image
infoLabel.Text="<u>Propriété de l'image </u><br>";
infoLabel.Text=infoLabel.Text + "<br>HorizontalResolution : " + monImage.HorizontalResolution;
infoLabel.Text=infoLabel.Text + "<br>VerticalResolution : " + monImage.VerticalResolution;
infoLabel.Text=infoLabel.Text + "<br>RawFormat : " + monImage.RawFormat;
infoLabel.Text=infoLabel.Text + "<br>PixelFormat : " + monImage.PixelFormat;
infoLabel.Text=infoLabel.Text + "<br>tailleImage : " + monImage.tailleImage;
infoLabel.Text=infoLabel.Text + "<br>Poid mon image :" + monImage.poidImage(path);
}
What gives:

|