Asp.net image scaling and watermarking program

Source: Internet
Author: User
The code is as follows: Copy code

/// <Summary>
/// Obtain the size of an image after proportional reduction.
/// </Summary>
/// <Param name = "maxWidth"> width to be reduced </param>
/// <Param name = "maxHeight"> height to be reduced </param>
/// <Param name = "imageOriginalWidth"> original image width </param>
/// <Param name = "imageOriginalHeight"> original image height </param>
/// <Returns> the actual size of the returned image after the proportional reduction </returns>
Public static Size GetNewSize (int maxWidth, int maxHeight, int imageOriginalWidth, int imageOriginalHeight)
{
Double w = 0.0;
Double h = 0.0;
Double sw = Convert. ToDouble (imageOriginalWidth );
Double sh = Convert. ToDouble (imageOriginalHeight );
Double mw = Convert. ToDouble (maxWidth );
Double mh = Convert. ToDouble (maxHeight );
If (sw <mw & sh <mh)
    {
W = sw;
H = sh;
    }
Else if (sw/sh)> (mw/mh ))
    {
W = maxWidth;
H = (w * sh)/sw;
    }
Else
    {
H = maxHeight;
W = (h * sw)/sh;
    }
Return new Size (Convert. ToInt32 (w), Convert. ToInt32 (h ));
}

/// <Summary>
/// Generate a thumbnail of the specified size for a given Image object.
/// </Summary>
/// <Param name = "originalImage"> Original Image </param>
/// <Param name = "thumMaxWidth"> width of the thumbnail </param>
/// <Param name = "thumMaxHeight"> height of the thumbnail </param>
/// <Returns> returns the Image object of the thumbnail </returns>
Public static System. Drawing. Image GetThumbNailImage (System. Drawing. Image originalImage, int thumMaxWidth, int thumMaxHeight)
{
Size thumRealSize = Size. Empty;
System. Drawing. Image newImage = originalImage;
Graphics graphics = null;
Try
    {
ThumRealSize = GetNewSize (thumMaxWidth, thumMaxHeight, originalImage. Width, originalImage. Height );
NewImage = new Bitmap (thumRealSize. Width, thumRealSize. Height );
Graphics = Graphics. FromImage (newImage );
Graphics. CompositingQuality = CompositingQuality. HighQuality;
Graphics. InterpolationMode = InterpolationMode. HighQualityBicubic;
Graphics. SmoothingMode = SmoothingMode. HighQuality;
Graphics. Clear (Color. Transparent );
Graphics. drawImage (originalImage, new Rectangle (0, 0, thumRealSize. width, thumRealSize. height), new Rectangle (0, 0, originalImage. width, originalImage. height), GraphicsUnit. pixel );
    }
Catch {}
Finally
    {
If (graphics! = Null)
        {
Graphics. Dispose ();
Graphics = null;
        }
    }
Return newImage;
}

/// <Summary>
/// Generate a thumbnail of the specified size for a given image file.
/// </Summary>
/// <Param name = "originalImage"> address of the physical file of the image </param>
/// <Param name = "thumMaxWidth"> width of the thumbnail </param>
/// <Param name = "thumMaxHeight"> height of the thumbnail </param>
/// <Returns> returns the Image object of the thumbnail </returns>
Public static System. Drawing. Image GetThumbNailImage (string imageFile, int thumMaxWidth, int thumMaxHeight)
{
System. Drawing. Image originalImage = null;
System. Drawing. Image newImage = null;
Try
    {
OriginalImage = System. Drawing. Image. FromFile (imageFile );
NewImage = GetThumbNailImage (originalImage, thumMaxWidth, thumMaxHeight );
    }
Catch {}
Finally
    {
If (originalImage! = Null)
        {
OriginalImage. Dispose ();
OriginalImage = null;
        }
    }
Return newImage;
}

/// <Summary>
/// Generate a thumbnail of the specified size for a given image file and save the thumbnail to the specified position.
/// </Summary>
/// <Param name = "originalImageFile"> address of the physical file of the image </param>
/// <Param name = "thumbNailImageFile"> physical file address of the thumbnail </param>
/// <Param name = "thumMaxWidth"> width of the thumbnail </param>
/// <Param name = "thumMaxHeight"> height of the thumbnail </param>
Public static void MakeThumbNail (string originalImageFile, string thumbNailImageFile, int thumMaxWidth, int thumMaxHeight)
{
System. Drawing. Image newImage = GetThumbNailImage (originalImageFile, thumMaxWidth, thumMaxHeight );
Try
    {
NewImage. Save (thumbNailImageFile, ImageFormat. Jpeg );
    }
Catch
{}
Finally
    {
NewImage. Dispose ();
NewImage = null;
    }
}

/// <Summary>
/// Adjust the memory stream of an image to the specified size and return the adjusted memory stream.
/// </Summary>
/// <Param name = "originalImageStream"> memory stream of the original image </param>
/// <Param name = "newWidth"> width of the new image </param>
/// <Param name = "newHeight"> height of the new image </param>
/// <Returns> returns the memory stream of the adjusted Image. </returns>
Public static MemoryStream ResizeImage (Stream originalImageStream, int newWidth, int newHeight)
{
MemoryStream newImageStream = null;
System. Drawing. Image newImage = GetThumbNailImage (System. Drawing. Image. FromStream (originalImageStream), newWidth, newHeight );
If (newImage! = Null)
    {
NewImageStream = new MemoryStream ();
NewImage. Save (newImageStream, ImageFormat. Jpeg );
    }
Return newImageStream;
}

/// <Summary>
/// Save a memory stream as a disk file.
/// </Summary>
/// <Param name = "stream"> memory stream </param>
/// <Param name = "newFile"> Target Disk file address </param>
Public static void SaveStreamToFile (Stream stream, string newFile)
{
If (stream = null | stream. Length = 0 | string. IsNullOrEmpty (newFile ))
    {
Return;
    }
Byte [] buffer = new byte [stream. Length];
Stream. Position = 0;
Stream. Read (buffer, 0, buffer. Length );
FileStream fileStream = new FileStream (newFile, FileMode. OpenOrCreate, FileAccess. Write );
FileStream. Write (buffer, 0, buffer. Length );
FileStream. Flush ();
FileStream. Close ();
FileStream. Dispose ();
}

/// <Summary>
/// Add an image watermark to a specified image.
/// </Summary>
/// <Param name = "imageFile"> Image file address </param>
/// <Param name = "waterImage"> watermark Image (Image object) </param>
Public static void CreateImageWaterMark (string imageFile, System. Drawing. Image waterImage)
{
If (string. IsNullOrEmpty (imageFile) |! File. Exists (imageFile) | waterImage = null)
    {
Return;
    }
System. Drawing. Image originalImage = System. Drawing. Image. FromFile (imageFile );
If (originalImage. Width-10 <waterImage. Width | originalImage. Height-10 <waterImage. Height)
    {
Return;
    }
Graphics graphics = Graphics. FromImage (originalImage );
Int x = originalImage. Width-waterImage. Width-10;
Int y = originalImage. Height-waterImage. Height-10;
Int width = waterImage. Width;
Int height = waterImage. Height;
Graphics. DrawImage (waterImage, new Rectangle (x, y, width, height), 0, 0, width, height, GraphicsUnit. Pixel );
Graphics. Dispose ();
MemoryStream stream = new MemoryStream ();
OriginalImage. Save (stream, ImageFormat. Jpeg );
OriginalImage. Dispose ();
System. Drawing. Image imageWithWater = System. Drawing. Image. FromStream (stream );
ImageWithWater. Save (imageFile );
ImageWithWater. Dispose ();
}

/// <Summary>
/// Add a text watermark to a specified image.
/// </Summary>
/// <Param name = "imageFile"> Image file address </param>
/// <Param name = "waterText"> watermark text content </param>
Public static void CreateTextWaterMark (string imageFile, string waterText)
{
If (string. IsNullOrEmpty (imageFile) | string. IsNullOrEmpty (waterText) |! File. Exists (imageFile ))
    {
Return;
    }
System. Drawing. Image originalImage = System. Drawing. Image. FromFile (imageFile );
Graphics graphics = Graphics. FromImage (originalImage );
Graphics. SmoothingMode = SmoothingMode. HighQuality;
Graphics. TextRenderingHint = TextRenderingHint. ClearTypeGridFit;
Graphics. CompositingQuality = CompositingQuality. HighQuality;
Graphics. InterpolationMode = InterpolationMode. HighQualityBicubic;
SolidBrush brush = new SolidBrush (Color. FromArgb (153,255,255,255 ));
Font waterTextFont = new Font ("Arial", 16, FontStyle. Regular );
SizeF waterTextSize = graphics. MeasureString (waterText, waterTextFont );
Float x = (float) originalImage. Width-waterTextSize. Width-10F;
Float y = (float) originalImage. Height-waterTextSize. Height-10F;
Graphics. DrawString (waterText, waterTextFont, brush, x, y );
Graphics. Dispose ();
Brush. Dispose ();
MemoryStream stream = new MemoryStream ();
OriginalImage. Save (stream, ImageFormat. Jpeg );
OriginalImage. Dispose ();
System. Drawing. Image imageWithWater = System. Drawing. Image. FromStream (stream );
ImageWithWater. Save (imageFile );
ImageWithWater. Dispose ();
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.