其实在GDI+中,缩放和剪裁可以看作同一个操作,无非就是原始区域的选择不同罢了。空口无凭,先看具体算法可能更好理解。
/// <summary>
/// Resize图片
/// </summary>
/// <param name="bmp">原始Bitmap</param>
/// <param name="newW">新的宽度</param>
/// <param name="newH">新的高度</param>
/// <param name="Mode">保留着,暂时未用</param>
/// <returns>处理以后的图片</returns>
public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH, int Mode)
{
try
{
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
{
return null;
}
}
// ===============================
/// <summary>
/// 剪裁 -- 用GDI+
/// </summary>
/// <param name="b">原始Bitmap</param>
/// <param name="StartX">开始坐标X</param>
/// <param name="StartY">开始坐标Y</param>
/// <param name="iWidth">宽度</param>
/// <param name="iHeight">高度</param>
/// <returns>剪裁后的Bitmap</returns>
public static Bitmap KiCut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
if (StartX >= w || StartY >= h)
{
return null;
}
if (StartX + iWidth > w)
{
iWidth = w - StartX;
}
if (StartY + iHeight > h)
{
iHeight = h - StartY;
}
try
{
Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
注意到区别了吗?提示,g.DrawImage中第二个new Rectangle。
目标其实都是new Rectangle(0, 0, iWidth, iHeight),缩放算法把整个原始图都往目标区域里塞new Rectangle(0, 0, bmp.Width, bmp.Height),而剪裁只是把原始区域上等宽等高的那个区域new Rectangle(StartX, StartY, iWidth, iHeight)1:1的塞到目标区域里。很容易吧。
ASP.NET 图片截取
//SendKeys.SendWait("%{PRTSC}"); // 模拟 Alt+PrtSc,如果要截取整个屏幕,把 Alt (%) 去掉
SendKeys.SendWait("Alt{PRTSC}");
// 获取剪贴板里的图像:
Bitmap myCapture = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
// 要显示图像可以这么做:
this.pictureBox1.Image = myCapture;
// 要保存图像可以这么做:
myCapture.Save(@"C:\myCapture.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
==========================================================================================================
private void Page_Load(object sender, System.EventArgs e)
{
ImgReduceCutOut(100,120,"1.jpg","2.jpg");
}
/// <summary>
/// 缩小裁剪图片
/// </summary>
/// <param name="int_Width">要缩小裁剪图片宽度</param>
/// <param name="int_Height">要缩小裁剪图片长度</param>
/// <param name="input_ImgUrl">要处理图片路径</param>
/// <param name="out_ImgUrl">处理完毕图片路径</param>
public void ImgReduceCutOut(int int_Width,int int_Height,string input_ImgUrl,string out_ImgUrl)
{
// ===上传标准图大小===
int int_Standard_Width=160;
int int_Standard_Height=160;
int Reduce_Width=0; // 缩小的宽度
int Reduce_Height=0; // 缩小的高度
int CutOut_Width=0; // 裁剪的宽度
int CutOut_Height=0; // 裁剪的高度
int level = 100; //缩略图的质量 1-100的范围
// ===获得缩小,裁剪大小===
if (int_Standard_Height*int_Width/int_Standard_Width>int_Height)
{
Reduce_Width=int_Width;
Reduce_Height=int_Standard_Height*int_Width/int_Standard_Width;
CutOut_Width=int_Width;
CutOut_Height=int_Height;
}
else if (int_Standard_Height*int_Width/int_Standard_Width<int_Height)
{
Reduce_Width=int_Standard_Width*int_Height/int_Standard_Height;
Reduce_Height=int_Height;
CutOut_Width=int_Width;
CutOut_Height=int_Height;
}
else
{
Reduce_Width=int_Width;
Reduce_Height=int_Height;
CutOut_Width=int_Width;
CutOut_Height=int_Height;
}
// ===通过连接创建Image对象===
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(input_ImgUrl));
// ===缩小图片===
System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(Reduce_Width, Reduce_Height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Bitmap bm=new Bitmap(thumbnailImage);
// ===处理JPG质量的函数===
ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici=null;
foreach(ImageCodecInfo codec in codecs)
{
if(codec.MimeType=="image/jpeg")
ici=codec;
}
EncoderParameters ep=new EncoderParameters();
ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)level);
bm.Save(Server.MapPath("2.jpg"),ici,ep);
// ===裁剪图片===
Rectangle cloneRect = new Rectangle(0, 0, CutOut_Width, CutOut_Height);
PixelFormat format = bm.PixelFormat;
Bitmap cloneBitmap = bm.Clone(cloneRect, format);
// ===保存图片===
cloneBitmap.Save(Server.MapPath(out_ImgUrl),ici,ep);
}
public bool ThumbnailCallback()
{
return false;
}
Asp.net中裁剪,缩小图片
private void Page_Load(object sender, System.EventArgs e)
{
ImgReduceCutOut(100,120,"1.jpg","2.jpg");
}
/// <summary>
/// 缩小裁剪图片
/// </summary>
/// <param name="int_Width">要缩小裁剪图片宽度</param>
/// <param name="int_Height">要缩小裁剪图片长度</param>
/// <param name="input_ImgUrl">要处理图片路径</param>
/// <param name="out_ImgUrl">处理完毕图片路径</param>
public void ImgReduceCutOut(int int_Width,int int_Height,string input_ImgUrl,string out_ImgUrl)
{
// ===上传标准图大小===
int int_Standard_Width=160;
int int_Standard_Height=160;
int Reduce_Width=0; // 缩小的宽度
int Reduce_Height=0; // 缩小的高度
int CutOut_Width=0; // 裁剪的宽度
int CutOut_Height=0; // 裁剪的高度
int level = 100; //缩略图的质量 1-100的范围
// ===获得缩小,裁剪大小===
if (int_Standard_Height*int_Width/int_Standard_Width>int_Height)
{
Reduce_Width=int_Width;
Reduce_Height=int_Standard_Height*int_Width/int_Standard_Width;
CutOut_Width=int_Width;
CutOut_Height=int_Height;
}
else if (int_Standard_Height*int_Width/int_Standard_Width<int_Height)
{
Reduce_Width=int_Standard_Width*int_Height/int_Standard_Height;
Reduce_Height=int_Height;
CutOut_Width=int_Width;
CutOut_Height=int_Height;
}
else
{
Reduce_Width=int_Width;
Reduce_Height=int_Height;
CutOut_Width=int_Width;
CutOut_Height=int_Height;
}
// ===通过连接创建Image对象===
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(input_ImgUrl));
// ===缩小图片===
System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(Reduce_Width, Reduce_Height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Bitmap bm=new Bitmap(thumbnailImage);
// ===处理JPG质量的函数===
ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici=null;
foreach(ImageCodecInfo codec in codecs)
{
if(codec.MimeType=="image/jpeg")
ici=codec;
}
EncoderParameters ep=new EncoderParameters();
ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)level);
bm.Save(Server.MapPath("2.jpg"),ici,ep);
// ===裁剪图片===
Rectangle cloneRect = new Rectangle(0, 0, CutOut_Width, CutOut_Height);
PixelFormat format = bm.PixelFormat;
Bitmap cloneBitmap = bm.Clone(cloneRect, format);
// ===保存图片===
cloneBitmap.Save(Server.MapPath(out_ImgUrl),ici,ep);
}
public bool ThumbnailCallback()
{
return false;
}