Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.IO;
Using System.Drawing.Imaging;
Using System.Drawing;
Using System.Web;
Namespace ZC. Utils
{
public static Class Imagehelper
{
#region Picture Format Conversion
<summary>
Image format Conversion
</summary>
<param name= "Orifilename" > Original file relative path </param>
<param name= "Desiredfilename" > Generate target file relative path </param>
<returns></returns>
JPG uses lossy compression, so JPG images may degrade image sharpness, and pixels will not degrade.
GIF uses lossless compression so GIF images do not degrade image sharpness and pixels, but GIF formats support only 256-color images.
public static bool Convertimage (string orifilename, String desiredfilename)
{
String extname = desiredfilename.substring (Desiredfilename.lastindexof ('. ') +1). ToLower ();
ImageFormat Desiredformat;
According to the extension name, specify ImageFormat
Switch (extname)
{
Case "BMP":
Desiredformat = imageformat.bmp;
Break
Case "GIF":
Desiredformat = Imageformat.gif;
Break
Case "JPEG":
Desiredformat = Imageformat.jpeg;
Break
Case "ico":
Desiredformat = Imageformat.icon;
Break
Case "PNG":
Desiredformat = Imageformat.png;
Break
Default
Desiredformat = Imageformat.jpeg;
Break
}
Try
{
System.Drawing.Image imgfile = System.Drawing.Image.FromFile (Webpathtran (orifilename));
Imgfile.save (Webpathtran (desiredfilename), Desiredformat);
return true;
}
Catch
{
return false;
}
}
#endregion
#region Picture Zoom
<summary>
Picture fixed size zoom
</summary>
<param name= "Orifilename" > source file relative Address </param>
<param name= "Desiredfilename" > Target file relative address </param>
<param name= "intwidth" > Target file width </param>
<param name= "intheight" > target file Height </param>
<param name= "ImageFormat" > picture file Formats </param>
public static bool Changeimagesize (string orifilename, string desiredfilename, int intwidth, int intheight, ImageFormat im Ageformat)
{
String Sourcefilenamestr =webpathtran (orifilename); Source Picture name Path
String transferfilenamestr = Webpathtran (desiredfilename); Destination Picture name Path
FileStream Myoutput =null;
Try
{
System.Drawing.Image.GetThumbnailImageAbort myabort = new System.Drawing.Image.GetThumbnailImageAbort (imageabort);
Image sourceimage = System.Drawing.Image.FromFile (orifilename);//Source Picture definition
Image targetimage = Sourceimage.getthumbnailimage (intwidth, Intheight, Myabort, IntPtr.Zero); Purpose Picture Definition
The Targetfilenamestr picture is relaxed to intwidth, and the height is intheight
Myoutput = new FileStream (Transferfilenamestr, FileMode.Create, FileAccess.Write, fileshare.write);
Targetimage.save (Myoutput, ImageFormat);
Myoutput.close ();
return true;
}
Catch
{
Myoutput.close ();
return false;
}
}
private static bool Imageabort ()
{
return false;
}
#endregion
#region Text watermark
<summary>
Text watermark
</summary>
<param name= "Wtext" > Watermark text </param>
<param name= "source" > Original picture physical file name </param>
<param name= "target" > Generate picture Physical file name </param>
public static bool Imagewatertext (string wtext,string source, string target)
{
BOOL Resflag = false;
Image image = Image.FromFile (source);
Graphics graphics = graphics.fromimage (image);
Try
{
Graphics. DrawImage (image, 0, 0, image. Width, image. Height);
Font font = new System.Drawing.Font ("Verdana", 60);
Brush brush = new System.Drawing.SolidBrush (System.Drawing.Color.Green);
Graphics. DrawString (wtext, font, brush, 35, 35);
Image. Save (target);
Resflag = true;
}
catch (Exception)
{
Throw
}
Finally
{
Graphics. Dispose ();
Image. Dispose ();
}
return resflag;
}
#endregion
#region Image Watermark
<summary>
Create a picture watermark on a picture
</summary>
<param name= "Path" > Original server picture Path </param>
<param name= "PATH_SYP" > Generated picture watermark with picture path </param>
<param name= "PATH_SYPF" > Watermark picture Path </param>
public static bool Imagewaterpic (string source, string target, String waterpicsource)
{
BOOL Resflag = false;
Image sourceimage = image.fromfile (source);
Graphics sourcegraphics = Graphics.fromimage (sourceimage);
Image waterpicsourceimage = Image.FromFile (Waterpicsource);
Try
{
Sourcegraphics. DrawImage (Waterpicsourceimage, New System.Drawing.Rectangle (sourceimage. Width-waterpicsourceimage.width, Sourceimage. Height-waterpicsourceimage.height, Waterpicsourceimage.width, Waterpicsourceimage.height), 0, 0, Waterpicsourceimage.width, Waterpicsourceimage.height, GraphicsUnit.Pixel);
Sourceimage. Save (target);
}
catch (Exception)
{
Throw
}
Finally
{
Sourcegraphics. Dispose ();
Sourceimage. Dispose ();
Waterpicsourceimage.dispose ();
}
return resflag;
}
#endregion
#region Path Conversion
<summary>
Path conversion (converted to absolute path)
</summary>
<param name= "Path" ></param>
<returns></returns>
private static string Webpathtran (string path)
{
Try
{
return HttpContext.Current.Server.MapPath (path);
}
Catch
{
return path;
}
}
#endregion
}
}
. NET picture operation class, including image format conversion, image zoom, text watermark, image watermark, path conversion