ASP tutorial. NET C # picture scaling enlarge Cut Code
using System;
Using System.Collections.Generic;
Using System.Text;
Using System.IO;
Using System.Drawing;
Using System.Drawing.Drawing2D;
Using System.Drawing.Imaging;
Namespace Wujian.common
{
<summary>
Picture Processing class
Wu Jian 2008-07-02 Created
Wu Jian 2011-01-21 Revision
</summary>
public class Ptimage
{
#region Square cropping and scaling
<summary>
Square type cropping
Take the center of the picture as the axis, intercept the square, and then zoom
for avatar Processing
</summary>
<remarks> Wu Jian 2010-11-23</remarks>
<param name= "PostedFile" > Original artwork Httppostedfile object </param>
<param name= "Filesaveurl" > Thumbnail store address </param>
<param name= "side" > Specified edges (square type) </param>
<param name= "Quality" > Quality (range 0-100) </param>
public static void Cutforsquare (System.web.httppostedfile postedfile, string filesaveurl, int side, int quality)
{
Create a table of contents
String dir = Path.getdirectoryname (Filesaveurl);
if (!directory.exists (dir))
Directory.CreateDirectory (dir);
Original picture (Gets the original picture creation object and uses the color management information embedded in the stream)
System.Drawing.Image initimage = System.drawing.image.fromstream (Postedfile.inputstream, true);
The original image width and height are less than the template, not for processing, direct storage
if (initimage.width <= side && initimage.height <= side)
{
Initimage.save (Filesaveurl, system.drawing.imaging.imageformat.jpeg);
}
Else
{
The width and height of the original picture
int initwidth = Initimage.width;
int initheight = Initimage.height;
Non-square type first cropped to square
if (initwidth!= initheight)
{
Screenshot object
System.Drawing.Image pickedimage = null;
System.Drawing.Graphics pickedg = null;
wide and high horizontal picture
if (Initwidth > Initheight)
{
Object instantiation
Pickedimage = new System.Drawing.Bitmap (initheight, initheight);
PICKEDG = System.drawing.graphics.fromimage (pickedimage);
Set quality
Pickedg.interpolationmode = System.drawing.drawing2d.interpolationmode.highqualitybicubic;
Pickedg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
Positioning
Rectangle FROMR = new Rectangle ((initwidth-initheight)/2, 0, Initheight, initheight);
Rectangle tor = new Rectangle (0, 0, initheight, initheight);
Drawing
Pickedg.drawimage (Initimage, Tor, FROMR, System.drawing.graphicsunit.pixel);
Reset width
Initwidth = Initheight;
}
Tall and wide Vertical chart
Else
{
Object instantiation
Pickedimage = new System.Drawing.Bitmap (initwidth, initwidth);
PICKEDG = System.drawing.graphics.fromimage (pickedimage);
Set quality
Pickedg.interpolationmode = System.drawing.drawing2d.interpolationmode.highqualitybicubic;
Pickedg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
Positioning
Rectangle FROMR = new Rectangle (0, (initheight-initwidth)/2, initwidth, initwidth);
Rectangle tor = new Rectangle (0, 0, initwidth, initwidth);
Drawing
Pickedg.drawimage (Initimage, Tor, FROMR, System.drawing.graphicsunit.pixel);
Reset High
Initheight = Initwidth;
}
Assign a screenshot object to the original artwork
Initimage = (system.drawing.image) pickedimage.clone ();
Release screenshot Resource
Pickedg.dispose ();
Pickedimage.dispose ();
}
Thumbnail objects
System.Drawing.Image resultimage = new System.Drawing.Bitmap (side, side);
System.Drawing.Graphics RESULTG = System.drawing.graphics.fromimage (resultimage);
Set quality
Resultg.interpolationmode = System.drawing.drawing2d.interpolationmode.highqualitybicubic;
Resultg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
Empty the canvas with the specified background color
Resultg.clear (Color.White);
Drawing thumbnails
Resultg.drawimage (Initimage, New System.Drawing.Rectangle (0, 0, side, side), new System.Drawing.Rectangle (0, 0, Initwidth, Initheight), system.drawing.graphicsunit.pixel);
Key quality Control
Gets an array of system encoded types, containing the Jpeg,bmp,png,gif,tiff
imagecodecinfo[] Icis = Imagecodecinfo.getimageencoders ();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo i in ICIS)
{
if (I.mimetype = = "Image/jpeg" | | i.mimetype = "Image/bmp" | | i.mimetype = "Image/png" | | i.mimetype = "Image/gif")
{
ICI = i;
}
}
EncoderParameters EP = new EncoderParameters (1);
Ep.param[0] = new Encoderparameter (system.drawing.imaging.encoder.quality, (long) quality);
Save thumbnails
Resultimage.save (Filesaveurl, ICI, EP);
Releasing resources for critical quality control
Ep.dispose ();
Releasing thumbnail resources
Resultg.dispose ();
Resultimage.dispose ();
Release Original picture Resource
Initimage.dispose ();
}
}
<summary>
Square type cropping
Take the center of the picture as the axis, intercept the square, and then zoom
for avatar Processing
</summary>
<remarks> Wu Jian 2010-11-23</remarks>
<param name= "PostedFile" > Original artwork Httppostedfile object </param>
<param name= "Filesaveurl" > Thumbnail store address </param>
<param name= "side" > Specified edges (square type) </param>
<param name= "Quality" > Quality (range 0-100) </param>
public static void Cutforsquare (System.IO.Stream fromfile, string filesaveurl, int side, int quality)
{
Create a table of contents
String dir = Path.getdirectoryname (Filesaveurl);
if (!directory.exists (dir))
Directory.CreateDirectory (dir);
Original picture (Gets the original picture creation object and uses the color management information embedded in the stream)
System.Drawing.Image initimage = System.drawing.image.fromstream (FromFile, true);
The original image width and height are less than the template, not for processing, direct storage
if (initimage.width <= side && initimage.height <= side)
{
Initimage.save (Filesaveurl, system.drawing.imaging.imageformat.jpeg);
}
Else
{
The width and height of the original picture
int initwidth = Initimage.width;
int initheight = Initimage.height;
Non-square type first cropped to square
if (initwidth!= initheight)
{
Screenshot object
System.Drawing.Image pickedimage = null;
System.Drawing.Graphics pickedg = null;
wide and high horizontal picture
if (Initwidth > Initheight)
{
Object instantiation
Pickedimage = new System.Drawing.Bitmap (initheight, initheight);
PICKEDG = System.drawing.graphics.fromimage (pickedimage);
Set quality
Pickedg.interpolationmode = System.drawing.drawing2d.interpolationmode.highqualitybicubic;
Pickedg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
Positioning
Rectangle FROMR = new Rectangle ((initwidth-initheight)/2, 0, Initheight, initheight);
Rectangle tor = new Rectangle (0, 0, initheight, initheight);
Drawing
Pickedg.drawimage (Initimage, Tor, FROMR, System.drawing.graphicsunit.pixel);
Reset width
Initwidth = Initheight;
}
Tall and wide Vertical chart
Else
{
Object instantiation
Pickedimage = new System.Drawing.Bitmap (initwidth, initwidth);
PICKEDG = System.drawing.graphics.fromimage (pickedimage);
Set quality
Pickedg.interpolationmode = System.drawing.drawing2d.interpolationmode.highqualitybicubic;
Pickedg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
Positioning
Rectangle FROMR = new Rectangle (0, (initheight-initwidth)/2, initwidth, initwidth);
Rectangle tor = new Rectangle (0, 0, initwidth, initwidth);
Drawing
Pickedg.drawimage (Initimage, Tor, FROMR, System.drawing.graphicsunit.pixel);
Reset High
Initheight = Initwidth;
}
Assign a screenshot object to the original artwork
Initimage = (system.drawing.image) pickedimage.clone ();
Release screenshot Resource
Pickedg.dispose ();
Pickedimage.dispose ();
}
Thumbnail objects
System.Drawing.Image resultimage = new System.Drawing.Bitmap (side, side);
System.Drawing.Graphics RESULTG = System.drawing.graphics.fromimage (resultimage);
Set quality
Resultg.interpolationmode = System.drawing.drawing2d.interpolationmode.highqualitybicubic;
Resultg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
Empty the canvas with the specified background color
Resultg.clear (Color.White);
Drawing thumbnails
Resultg.drawimage (Initimage, New System.Drawing.Rectangle (0, 0, side, side), new System.Drawing.Rectangle (0, 0, Initwidth, Initheight), system.drawing.graphicsunit.pixel);
Key quality Control
Gets an array of system encoded types, containing the Jpeg,bmp,png,gif,tiff
imagecodecinfo[] Icis = Imagecodecinfo.getimageencoders ();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo i in ICIS)
{
if (I.mimetype = = "Image/jpeg" | | i.mimetype = "Image/bmp" | | i.mimetype = "Image/png" | | i.mimetype = "Image/gif")
{
ICI = i;
}
}
EncoderParameters EP = new EncoderParameters (1);
Ep.param[0] = new Encoderparameter (system.drawing.imaging.encoder.quality, (long) quality);
Save thumbnails
Resultimage.save (Filesaveurl, ICI, EP);
Releasing resources for critical quality control
Ep.dispose ();
Releasing thumbnail resources
Resultg.dispose ();
Resultimage.dispose ();
Release Original picture Resource
Initimage.dispose ();
}
}
#endregion
Crop and scale #region a fixed stencil
<summary>
Specify long width cropping
Crop the picture by the largest scale and zoom to the stencil size
</summary>
<remarks> Wu Jian 2010-11-15</remarks>
<param name= "PostedFile" > Original artwork Httppostedfile object </param>
<param name= "Filesaveurl" > Save path </param>
<param name= "MaxWidth" > Max width (unit: px) </param>
<param name= "MaxHeight" > Maximum height (unit: PX) </param>
<param name= "Quality" > Quality (range 0-100) </param>
public static void Cutforcustom (System.web.httppostedfile postedfile, string filesaveurl, int maxwidth, int maxheight, in T quality)
{
Get the original picture from the file and use the color management information embedded in the stream
System.Drawing.Image initimage = System.drawing.image.fromstream (Postedfile.inputstream, true);
The original image width and height are less than the template, not for processing, direct storage
if (initimage.width <= maxwidth && initimage.height <= maxheight)
{
Initimage.save (Filesaveurl, system.drawing.imaging.imageformat.jpeg);
}
Else
{
The width and height ratio of the template
Double templaterate = (double) maxwidth/maxheight;
The width and height ratio of the original picture
Double initrate = (double) initimage.width/initimage.height;
The original image is equal to the template, directly scaling
if (templaterate = = initrate)
{
Generate final picture by stencil size
System.Drawing.Image templateimage = new System.Drawing.Bitmap (maxwidth, maxheight);
System.Drawing.Graphics Templateg = System.drawing.graphics.fromimage (templateimage);
Templateg.interpolationmode = System.drawing.drawing2d.interpolationmode.high;
Templateg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
Templateg.clear (Color.White);
Templateg.drawimage (Initimage, New System.Drawing.Rectangle (0, 0, MaxWidth, maxheight), New System.Drawing.Rectangle ( 0, 0, Initimage.width, initimage.height), system.drawing.graphicsunit.pixel);
Templateimage.save (Filesaveurl, system.drawing.imaging.imageformat.jpeg);
}
The original image and the template scale, after cropping scaling
Else
{
Cropping objects
System.Drawing.Image pickedimage = null;
System.Drawing.Graphics pickedg = null;
Positioning
Rectangle FROMR = new Rectangle (0, 0, 0, 0);//Original cut positioning
Rectangle tor = new Rectangle (0, 0, 0, 0);//target positioning
Wide for standard cropping
if (Templaterate > Initrate)
{
Cropping Object instantiation
Pickedimage = new System.Drawing.Bitmap (initimage.width, (int) Math.floor (initimage.width/templaterate));
PICKEDG = System.drawing.graphics.fromimage (pickedimage);
//Crop source positioning
fromr.x = 0;
fromr.y = (int) Math.floor ((initimage.height-initimage.width/templaterate)/2) ;
fromr.width = initimage.width;
fromr.height = (int) math.floor (initimage.width/templaterate);
Crop target positioning
tor.x = 0;
TOR.Y = 0;
Tor.width = Initimage.width;
Tor.height = (int) math.floor (initimage.width/templaterate);
}
High for the standard to cut
Else
{
Pickedimage = new System.Drawing.Bitmap ((int) Math.floor (initimage.height * templaterate), initimage.height);
PICKEDG = System.drawing.graphics.fromimage (pickedimage);
fromr.x = (int) math.floor (initimage.width-initimage.height * templaterate)/2);
FROMR.Y = 0;
Fromr.width = (int) math.floor (initimage.height * templaterate);
Fromr.height = Initimage.height;
tor.x = 0;
tor.y = 0;
tor.width = (int) math.floor (initimage.height * templaterate);
tor.height = initimage.height;
}
Set quality
Pickedg.interpolationmode = System.drawing.drawing2d.interpolationmode.highqualitybicubic;
Pickedg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
Cutting
Pickedg.drawimage (Initimage, Tor, FROMR, System.drawing.graphicsunit.pixel);
Generate final picture by stencil size
System.Drawing.Image templateimage = new System.Drawing.Bitmap (maxwidth, maxheight);
System.Drawing.Graphics Templateg = System.drawing.graphics.fromimage (templateimage);
Templateg.interpolationmode = System.drawing.drawing2d.interpolationmode.high;
Templateg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
Templateg.clear (Color.White);
Templateg.drawimage (Pickedimage, New System.Drawing.Rectangle (0, 0, MaxWidth, maxheight), new System.Drawing.Rectangle (0, 0, Pickedimage.width, pickedimage.height), system.drawing.graphicsunit.pixel);
Key quality Control
Gets an array of system encoded types, containing the Jpeg,bmp,png,gif,tiff
imagecodecinfo[] Icis = Imagecodecinfo.getimageencoders ();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo i in ICIS)
{
if (I.mimetype = = "Image/jpeg" | | i.mimetype = "Image/bmp" | | i.mimetype = "Image/png" | | i.mimetype = "Image/gif")
{
ICI = i;
}
}
EncoderParameters EP = new EncoderParameters (1);
Ep.param[0] = new Encoderparameter (system.drawing.imaging.encoder.quality, (long) quality);
Save thumbnails
Templateimage.save (Filesaveurl, ICI, EP);
Templateimage.save (Filesaveurl, system.drawing.imaging.imageformat.jpeg);
Releasing resources
Templateg.dispose ();
Templateimage.dispose ();
Pickedg.dispose ();
Pickedimage.dispose ();
}
}
Releasing resources
Initimage.dispose ();
}
#endregion
Scaling #region Equal ratio
<summary>
Picture equal scaling
</summary>
<remarks> Wu Jian 2011-01-21</remarks>
<param name= "PostedFile" > Original artwork Httppostedfile object </param>
<param name= "Savepath" > Thumbnail store address </param>
<param name= "Targetwidth" > maximum width specified </param>
<param name= "Targetheight" > max height specified </param>
<param name= "Watermarktext" > Watermark text (for "" means no watermark) </param>
<param name= "Watermarkimage" > Watermark Image Path (for "" means no watermark) </param>
public static void Zoomauto (System.web.httppostedfile postedfile, String savepath, System.Double targetwidth, System.Double targetheight, String watermarktext, String watermarkimage)
{
Create a table of contents
String dir = Path.getdirectoryname (Savepath);
if (!directory.exists (dir))
Directory.CreateDirectory (dir);
Original picture (Gets the original picture creation object and uses the color management information embedded in the stream)
System.Drawing.Image initimage = System.drawing.image.fromstream (Postedfile.inputstream, true);
The original image width and height are less than the template, not for processing, direct storage
if (initimage.width <= targetwidth && initimage.height <= targetheight)
{
Text watermark
if (Watermarktext!= "")
{
using (System.Drawing.Graphics Gwater = System.drawing.graphics.fromimage (initimage))
{
System.Drawing.Font fontwater = new Font ("bold", 10);
System.Drawing.Brush brushwater = new SolidBrush (color.white);
Gwater.drawstring (Watermarktext, Fontwater, Brushwater, 10, 10);
Gwater.dispose ();
}
}
Transparent picture watermark
if (watermarkimage!= "")
{
if (file.exists (watermarkimage))
{
Get a watermark Picture
using (System.Drawing.Image wrimage = System.drawing.image.fromfile (watermarkimage))
{
Watermark Drawing Condition: The original picture width is greater than or equal to the watermark picture
if (initimage.width >= wrimage.width && initimage.height >= wrimage.height)
{
Graphics gwater = Graphics.fromimage (initimage);
Transparent properties
ImageAttributes imgattributes = new ImageAttributes ();
ColorMap ColorMap = new ColorMap ();
Colormap.oldcolor = Color.FromArgb (255, 0, 255, 0);
Colormap.newcolor = Color.FromArgb (0, 0, 0, 0);
Colormap[] remaptable = {ColorMap};
Imgattributes.setremaptable (remaptable, Coloradjusttype.bitmap);
Float[][] Colormatrixelements = {
New float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
New float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
New float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
New float[] {0.0f, 0.0f, 0.0f, 0.5f, 0.0f},//transparency: 0.5
New float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix Wmcolormatrix = New ColorMatrix (colormatrixelements);
Imgattributes.setcolormatrix (Wmcolormatrix, Colormatrixflag.default, Coloradjusttype.bitmap);
Gwater.drawimage (Wrimage, New Rectangle (initimage.width-wrimage.width, Initimage.height-wrimage.height, Wrimage.width, Wrimage.height), 0, 0, W Rimage.width, Wrimage.height, GraphicsUnit.Pixel, imgattributes);
Gwater.dispose ();
}
Wrimage.dispose ();
}
}
}
Save
Initimage.save (Savepath, system.drawing.imaging.imageformat.jpeg);
}
Else
{
Thumbnail width, height calculation
Double newwidth = initimage.width;
Double newheight = initimage.height;
High or wide equal to high (horizontal or square)
if (Initimage.width > Initimage.height | | | initimage.width = = initimage.height)
{
If you are more generous than the template
if (Initimage.width > Targetwidth)
{
Wide press template, high scaling
Newwidth = Targetwidth;
Newheight = Initimage.height * (targetwidth/initimage.width);
}
}
Tall to wide (vertical)
Else
{
If you are taller than the template
if (Initimage.height > Targetheight)
{
High press stencil, wide scaling
Newheight = Targetheight;
Newwidth = Initimage.width * (targetheight/initimage.height);
}
}
//Generate new diagram
//Create a new BMP picture
System.Drawing.Image NewImage = new System.Drawing.Bitmap ((int) newwidth, (int) newheight);
//Create a new artboard
System.Drawing.Graphics NEWG = System.drawing.graphics.fromimage (newimage);
//Set quality
Newg.interpolationmode = System.drawing.drawing2d.interpolationmode.highqualitybicubic;
Newg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
Set Background color
Newg.clear (Color.White);
Drawing
Newg.drawimage (Initimage, New System.Drawing.Rectangle (0, 0, Newimage.width, newimage.height), new System.Drawing.Rectangle (0, 0, Initimage.width, initimage.height), system.drawing.graphicsunit.pixel);
Text watermark
if (Watermarktext!= "")
{
using (System.Drawing.Graphics Gwater = System.drawing.graphics.fromimage (newimage))
{
System.Drawing.Font fontwater = new Font ("Song Body", 10);
System.Drawing.Brush brushwater = new SolidBrush (color.white);
Gwater.drawstring (Watermarktext, Fontwater, Brushwater, 10, 10);
Gwater.dispose ();
}
}
Transparent picture watermark
if (watermarkimage!= "")
{
if (file.exists (watermarkimage))
{
Get a watermark Picture
using (System.Drawing.Image wrimage = System.drawing.image.fromfile (watermarkimage))
{
Watermark Drawing Condition: The original picture width is greater than or equal to the watermark picture
if (newimage.width >= wrimage.width && newimage.height >= wrimage.height)
{
Graphics gwater = Graphics.fromimage (newimage);
Transparent properties
ImageAttributes imgattributes = new ImageAttributes ();
ColorMap ColorMap = new ColorMap ();
Colormap.oldcolor = Color.FromArgb (255, 0, 255, 0);
Colormap.newcolor = Color.FromArgb (0, 0, 0, 0);
Colormap[] remaptable = {ColorMap};
Imgattributes.setremaptable (remaptable, Coloradjusttype.bitmap);
Float[][] Colormatrixelements = {
New float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
New float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
New float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
New float[] {0.0f, 0.0f, 0.0f, 0.5f, 0.0f},//transparency: 0.5
New float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix Wmcolormatrix = new ColorMatrix (colormatrixelements);
Imgattributes.setcolormatrix (Wmcolormatrix, Colormatrixflag.default, Coloradjusttype.bitmap);
Gwater.drawimage (Wrimage, new Rectangle (newimage.width-wrimage.width, Newimage.height-wrimage.height, Wrimage.width, Wrimage.height), 0, 0, wrimage.width, Wrimage.height, GraphicsUnit.Pixel, imgattributes);
Gwater.dispose ();
}
Wrimage.dispose ();
}
}
}
Save thumbnails
Newimage.save (Savepath, system.drawing.imaging.imageformat.jpeg);
Releasing resources
Newg.dispose ();
Newimage.dispose ();
Initimage.dispose ();
}
}
#endregion
#region Other
<summary>
To determine whether a file type is a Web-formatted picture
(Note: jpg,gif,bmp,png)
</summary>
<param name= "contenttype" >httppostedfile.contenttype</param>
<returns></returns>
public static bool Iswebimage (string contenttype)
{
if (ContentType = = "Image/pjpeg" | | contenttype = "IMAGE/JPEG" | | contenttype = "Image/gif" | | contenttype = "Image/bmp" " || ContentType = = "Image/png")
{
return true;
}
Else
{
return false;
}
}
#endregion
}//end class
}
For example, the photo album feature, usually when the user uploads a photo, we will regenerate the photo to a thumbnail, for other pages on the list display. A casual look, most of the site is basically the original image, such as scaling to generate thumbnails. But the perfectionist will find some problems, such as the display of typesetting to make the photo thumbnail list is very uniform, neat, and beautiful, such as requiring each thumbnail size fixed to x 90 and do not stretch deformation how to do? How can a user avatar make a thumbnail more clear than the original image? Or how to add a translucent logo watermark under the uploaded image?
OK, this article according to their own project code description of the above problem solution, the so-called C # image processing advanced application, feel a bit of the title party, these features are not much technical content. All based on the. NET Framework class library complete, the code contains some basic knowledge of C # picture processing, share with you, personal ability is limited, also please correct me in time.