c image scaling image clipping function to achieve of proportional scaling
- 2020-05-30 20:59:37
- OfStack
The so-called c# image processing advanced should be done mostly based on the.net framework class library
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>
/// Image processing class
/// </summary>
public class ptimage
{
#region Square cut and scale
/// <summary>
/// Square cut
/// Take the center of the image as the axis, intercept the square shape, and then scale equably
/// For avatars
/// </summary>
/// <param name="postedfile"> The original image httppostedfile object </param>
/// <param name="filesaveurl"> Thumbnail location </param>
/// <param name="side"> Specified side length (tetragonal) </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 directory
string dir = path.getdirectoryname(filesaveurl);
if (!directory.exists(dir))
directory.createdirectory(dir);
// Raw image (get the original image create object and use the color management information embedded in the stream)
system.drawing.image initimage = system.drawing.image.fromstream(postedfile.inputstream, true);
// Original image width and height are smaller than the template, do not handle, directly saved
if (initimage.width <= side && initimage.height <= side)
{
initimage.save(filesaveurl, system.drawing.imaging.imageformat.jpeg);
}
else
{
// The width and height of the original image
int initwidth = initimage.width;
int initheight = initimage.height;
// The non - square cut first to the square
if (initwidth != initheight)
{
// Screenshot of the object
system.drawing.image pickedimage = null;
system.drawing.graphics pickedg = null;
// A broad than a high cross
if (initwidth > initheight)
{
// Object instantiation
pickedimage = new system.drawing.bitmap(initheight, initheight);
pickedg = system.drawing.graphics.fromimage(pickedimage);
// Set up the 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 the wide
initwidth = initheight;
}
// A vertical diagram larger than the width
else
{
// Object instantiation
pickedimage = new system.drawing.bitmap(initwidth, initwidth);
pickedg = system.drawing.graphics.fromimage(pickedimage);
// Set up the 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 the high
initheight = initwidth;
}
// Assign the screenshot object to the original
initimage = (system.drawing.image)pickedimage.clone();
// Release screenshot resources
pickedg.dispose();
pickedimage.dispose();
}
// Thumbnail object
system.drawing.image resultimage = new system.drawing.bitmap(side, side);
system.drawing.graphics resultg = system.drawing.graphics.fromimage(resultimage);
// Set up the 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);
// Draw thumbnails
resultg.drawimage(initimage, new system.drawing.rectangle(0, 0, side, side), new system.drawing.rectangle(0, 0, initwidth, initheight), system.drawing.graphicsunit.pixel);
// Critical quality control
// Gets an array of system encoding types , Contains 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 the thumbnail
resultimage.save(filesaveurl, ici, ep);
// Release key quality control resources
ep.dispose();
// Release the thumbnail resource
resultg.dispose();
resultimage.dispose();
// Release the original image resource
initimage.dispose();
}
}
/// <summary>
/// Square cut
/// Take the center of the image as the axis, intercept the square shape, and then scale equably
/// For avatars
/// </summary>
/// <param name="postedfile"> The original image httppostedfile object </param>
/// <param name="filesaveurl"> Thumbnail location </param>
/// <param name="side"> Specified side length (tetragonal) </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 directory
string dir = path.getdirectoryname(filesaveurl);
if (!directory.exists(dir))
directory.createdirectory(dir);
// Raw image (get the original image create object and use the color management information embedded in the stream)
system.drawing.image initimage = system.drawing.image.fromstream(fromfile, true);
// Original image width and height are smaller than the template, do not handle, directly saved
if (initimage.width <= side && initimage.height <= side)
{
initimage.save(filesaveurl, system.drawing.imaging.imageformat.jpeg);
}
else
{
// The width and height of the original image
int initwidth = initimage.width;
int initheight = initimage.height;
// The non - square cut first to the square
if (initwidth != initheight)
{
// Screenshot of the object
system.drawing.image pickedimage = null;
system.drawing.graphics pickedg = null;
// A broad than a high cross
if (initwidth > initheight)
{
// Object instantiation
pickedimage = new system.drawing.bitmap(initheight, initheight);
pickedg = system.drawing.graphics.fromimage(pickedimage);
// Set up the 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 the wide
initwidth = initheight;
}
// A vertical diagram larger than the width
else
{
// Object instantiation
pickedimage = new system.drawing.bitmap(initwidth, initwidth);
pickedg = system.drawing.graphics.fromimage(pickedimage);
// Set up the 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 the high
initheight = initwidth;
}
// Assign the screenshot object to the original
initimage = (system.drawing.image)pickedimage.clone();
// Release screenshot resources
pickedg.dispose();
pickedimage.dispose();
}
// Thumbnail object
system.drawing.image resultimage = new system.drawing.bitmap(side, side);
system.drawing.graphics resultg = system.drawing.graphics.fromimage(resultimage);
// Set up the 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);
// Draw thumbnails
resultg.drawimage(initimage, new system.drawing.rectangle(0, 0, side, side), new system.drawing.rectangle(0, 0, initwidth, initheight), system.drawing.graphicsunit.pixel);
// Critical quality control
// Gets an array of system encoding types , Contains 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 the thumbnail
resultimage.save(filesaveurl, ici, ep);
// Release key quality control resources
ep.dispose();
// Release the thumbnail resource
resultg.dispose();
resultimage.dispose();
// Release the original image resource
initimage.dispose();
}
}
#endregion
#region Fixed template clipping and scaling
/// <summary>
/// Specify width and length clipping
/// Crop the image to the maximum range of template proportions and scale to template size
/// </summary>
/// <param name="postedfile"> The original image httppostedfile object </param>
/// <param name="filesaveurl"> Save the path </param>
/// <param name="maxwidth"> Maximum width ( unit :px)</param>
/// <param name="maxheight"> The largest high ( 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, int quality)
{
// Get the original image from the file and use the color management information embedded in the stream
system.drawing.image initimage = system.drawing.image.fromstream(postedfile.inputstream, true);
// Original image width and height are smaller than the template, do not handle, directly saved
if (initimage.width <= maxwidth && initimage.height <= maxheight)
{
initimage.save(filesaveurl, system.drawing.imaging.imageformat.jpeg);
}
else
{
// The ratio of width to height of a template
double templaterate = (double)maxwidth / maxheight;
// The ratio of width to height of the original image
double initrate = (double)initimage.width / initimage.height;
// The original image is proportional to the template and scaled directly
if (templaterate == initrate)
{
// Generate the final image by template 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 scale of the original and template is not equal
else
{
// Cutting object
system.drawing.image pickedimage = null;
system.drawing.graphics pickedg = null;
// positioning
rectangle fromr = new rectangle(0, 0, 0, 0);// Clipping and positioning the original drawing
rectangle tor = new rectangle(0, 0, 0, 0);// target
// The width is cut for the standard
if (templaterate > initrate)
{
// Clipping object instantiation
pickedimage = new system.drawing.bitmap(initimage.width, (int)math.floor(initimage.width / templaterate));
pickedg = system.drawing.graphics.fromimage(pickedimage);
// Clipping source location
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);
// Clipping target location
tor.x = 0;
tor.y = 0;
tor.width = initimage.width;
tor.height = (int)math.floor(initimage.width / templaterate);
}
// Height is the standard for cutting
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 up the quality
pickedg.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
pickedg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
// tailoring
pickedg.drawimage(initimage, tor, fromr, system.drawing.graphicsunit.pixel);
// Generate the final image by template 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);
// Critical quality control
// Gets an array of system encoding types , Contains 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 the thumbnail
templateimage.save(filesaveurl, ici, ep);
//templateimage.save(filesaveurl, system.drawing.imaging.imageformat.jpeg);
// Release resources
templateg.dispose();
templateimage.dispose();
pickedg.dispose();
pickedimage.dispose();
}
}
// Release resources
initimage.dispose();
}
#endregion
#region Geometric scaling
/// <summary>
/// Scale the image proportionally
/// </summary>
/// <param name="postedfile"> The original image httppostedfile object </param>
/// <param name="savepath"> Thumbnail location </param>
/// <param name="targetwidth"> Specified maximum width </param>
/// <param name="targetheight"> The maximum height specified </param>
/// <param name="watermarktext"> Watermark text ( for "" Means no watermark is used )</param>
/// <param name="watermarkimage"> Watermark image path ( for "" Means no watermark is used )</param>
public static void zoomauto(system.web.httppostedfile postedfile, string savepath, system.double targetwidth, system.double targetheight, string watermarktext, string watermarkimage)
{
// Create a directory
string dir = path.getdirectoryname(savepath);
if (!directory.exists(dir))
directory.createdirectory(dir);
// Raw image (get the original image create object and use the color management information embedded in the stream)
system.drawing.image initimage = system.drawing.image.fromstream(postedfile.inputstream, true);
// Original image width and height are smaller than the template, do not handle, directly saved
if (initimage.width <= targetwidth && initimage.height <= targetheight)
{
// Text watermarking
if (watermarktext != "")
{
using (system.drawing.graphics gwater = system.drawing.graphics.fromimage(initimage))
{
system.drawing.font fontwater = new font(" blackbody ", 10);
system.drawing.brush brushwater = new solidbrush(color.white);
gwater.drawstring(watermarktext, fontwater, brushwater, 10, 10);
gwater.dispose();
}
}
// Transparent image watermarking
if (watermarkimage != "")
{
if (file.exists(watermarkimage))
{
// Get watermark image
using (system.drawing.image wrimage = system.drawing.image.fromfile(watermarkimage))
{
// Watermark drawing conditions: the original image width and height are greater than or equal to the watermark image
if (initimage.width >= wrimage.width && initimage.height >= wrimage.height)
{
graphics gwater = graphics.fromimage(initimage);
// Transparent property
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, wrimage.width, wrimage.height, graphicsunit.pixel, imgattributes);
gwater.dispose();
}
wrimage.dispose();
}
}
}
// save
initimage.save(savepath, system.drawing.imaging.imageformat.jpeg);
}
else
{
// Thumbnail width and height calculation
double newwidth = initimage.width;
double newheight = initimage.height;
// To be equal to (a cross or square) in height or width.
if (initimage.width > initimage.height || initimage.width == initimage.height)
{
// If larger than the template
if (initimage.width > targetwidth)
{
// Width to template, height to scale
newwidth = targetwidth;
newheight = initimage.height * (targetwidth / initimage.width);
}
}
// Taller than wider (portrait)
else
{
// If it's bigger than the template
if (initimage.height > targetheight)
{
// Height to template, width to scale
newheight = targetheight;
newwidth = initimage.width * (targetheight / initimage.height);
}
}
// Generate new
// new 1 a bmp The picture
system.drawing.image newimage = new system.drawing.bitmap((int)newwidth, (int)newheight);
// new 1 A drawing board
system.drawing.graphics newg = system.drawing.graphics.fromimage(newimage);
// Set up the quality
newg.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
newg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
// Set the background color
&nbs