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
{
public
class
Image
{
#region 正方型裁剪并缩放
public
static
void CutForSquare(System.IO.Stream fromFile, string fileSaveUrl, int side, int quality)
{
string dir = Path.GetDirectoryName(fileSaveUrl);
if
(!Directory.Exists(dir))
Directory.CreateDirectory(dir);
System.Drawing.Image initImage = System.Drawing.Image.FromStream(fromFile, true);
if
(initImage.Width <= side && initImage.Height <= side)
{
initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
int initWidth = initImage.Width;
int initHeight = initImage.Height;
if
(initWidth != initHeight)
{
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null;
if
(initWidth > initHeight)
{
pickedImage =
new
System.Drawing.Bitmap(initHeight, initHeight);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Rectangle fromR =
new
Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight);
Rectangle toR =
new
Rectangle(0, 0, initHeight, initHeight);
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
initWidth = initHeight;
}
else
{
pickedImage =
new
System.Drawing.Bitmap(initWidth, initWidth);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Rectangle fromR =
new
Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth);
Rectangle toR =
new
Rectangle(0, 0, initWidth, initWidth);
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
initHeight = initWidth;
}
initImage = (System.Drawing.Image)pickedImage.Clone();
pickedG.Dispose();
pickedImage.Dispose();
}
System.Drawing.Image resultImage =
new
System.Drawing.Bitmap(side, side);
System.Drawing.Graphics resultG = System.Drawing.Graphics.FromImage(resultImage);
resultG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
resultG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
resultG.Clear(Color.White);
resultG.DrawImage(initImage,
new
System.Drawing.Rectangle(0, 0, side, side),
new
System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel);
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);
resultImage.Save(fileSaveUrl, ici, ep);
ep.Dispose();
resultG.Dispose();
resultImage.Dispose();
initImage.Dispose();
}
}
#endregion
#region 自定义裁剪并缩放
public
static
void CutForCustom(System.IO.Stream fromFile, string fileSaveUrl, int maxWidth, int maxHeight, int quality)
{
System.Drawing.Image initImage = System.Drawing.Image.FromStream(fromFile, true);
if
(initImage.Width <= maxWidth && initImage.Height <= maxHeight)
{
initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
double templateRate = (double)maxWidth / maxHeight;
double initRate = (double)initImage.Width / initImage.Height;
if
(templateRate == initRate)
{
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);
}
else
{
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null;
Rectangle fromR =
new
Rectangle(0, 0, 0, 0);
Rectangle toR =
new
Rectangle(0, 0, 0, 0);
if
(templateRate > initRate)
{
pickedImage =
new
System.Drawing.Bitmap(initImage.Width, (int)System.Math.
Floor
(initImage.Width / templateRate));
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
fromR.X = 0;
fromR.Y = (int)System.Math.
Floor
((initImage.Height - initImage.Width / templateRate) / 2);
fromR.Width = initImage.Width;
fromR.Height = (int)System.Math.
Floor
(initImage.Width / templateRate);
toR.X = 0;
toR.Y = 0;
toR.Width = initImage.Width;
toR.Height = (int)System.Math.
Floor
(initImage.Width / templateRate);
}
else
{
pickedImage =
new
System.Drawing.Bitmap((int)System.Math.
Floor
(initImage.Height * templateRate), initImage.Height);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
fromR.X = (int)System.Math.
Floor
((initImage.Width - initImage.Height * templateRate) / 2);
fromR.Y = 0;
fromR.Width = (int)System.Math.
Floor
(initImage.Height * templateRate);
fromR.Height = initImage.Height;
toR.X = 0;
toR.Y = 0;
toR.Width = (int)System.Math.
Floor
(initImage.Height * templateRate);
toR.Height = initImage.Height;
}
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
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);
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);
templateImage.Save(fileSaveUrl, ici, ep);
templateG.Dispose();
templateImage.Dispose();
pickedG.Dispose();
pickedImage.Dispose();
}
}
initImage.Dispose();
}
#endregion
#region 等比缩放
public
static
void ZoomAuto(System.IO.Stream fromFile, string savePath, System.Double targetWidth, System.Double targetHeight, string watermarkText, string watermarkImage)
{
string dir = Path.GetDirectoryName(savePath);
if
(!Directory.Exists(dir))
Directory.CreateDirectory(dir);
System.Drawing.Image initImage = System.Drawing.Image.FromStream(fromFile, true);
if
(initImage.Width <= targetWidth && initImage.Height <= targetHeight)
{
if
(watermarkText !=
""
)
{
using (System.Drawing.Graphics gWater = System.Drawing.Graphics.FromImage(initImage))
{
System.Drawing.Font fontWater =
new
Font(
"黑体"
, 10);
System.Drawing.Brush brushWater =
new
SolidBrush(Color.White);
gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10);
gWater.Dispose();
}
}
if
(watermarkImage !=
""
)
{
if
(File.Exists(watermarkImage))
{
using (System.Drawing.Image wrImage = System.Drawing.Image.FromFile(watermarkImage))
{
if
(initImage.Width >= wrImage.Width && initImage.Height >= wrImage.Height)
{
Graphics gWater = Graphics.FromImage(initImage);
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},
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();
}
}
}
initImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
double newWidth = initImage.Width;
double newHeight = initImage.Height;
if
(initImage.Width > initImage.Height || initImage.Width == initImage.Height)
{
if
(initImage.Width > targetWidth)
{
newWidth = targetWidth;
newHeight = initImage.Height * (targetWidth / initImage.Width);
}
}
else
{
if
(initImage.Height > targetHeight)
{
newHeight = targetHeight;
newWidth = initImage.Width * (targetHeight / initImage.Height);
}
}
System.Drawing.Image newImage =
new
System.Drawing.Bitmap((int)newWidth, (int)newHeight);
System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);
newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
newG.Clear(Color.White);
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);
if
(watermarkText !=
""
)
{
using (System.Drawing.Graphics gWater = System.Drawing.Graphics.FromImage(newImage))
{
System.Drawing.Font fontWater =
new
Font(
"宋体"
, 10);
System.Drawing.Brush brushWater =
new
SolidBrush(Color.White);
gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10);
gWater.Dispose();
}
}
if
(watermarkImage !=
""
)
{
if
(File.Exists(watermarkImage))
{
using (System.Drawing.Image wrImage = System.Drawing.Image.FromFile(watermarkImage))
{
if
(newImage.Width >= wrImage.Width && newImage.Height >= wrImage.Height)
{
Graphics gWater = Graphics.FromImage(newImage);
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},
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();
}
}
}
newImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
newG.Dispose();
newImage.Dispose();
initImage.Dispose();
}
}
#endregion
#region 其它
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
}
}