c#截取图片

简单的保存数据流

<input name="upImg" style="width: 350px; height: 25px;" size="38" type="file" />
/// <summary>
/// 上传
/// </summary>
/// <param name="upImg"></param>
/// <returns></returns>
public JsonResult Upload(HttpPostedFileBase upImg)
{
string fileName = System.IO.Path.GetFileName(upImg.FileName);
string filePhysicalPath = Server.MapPath("~/Upload/" + fileName);
string pic = "";
int width = , height = ;
int error = ;
try
{
upImg.SaveAs(filePhysicalPath);
pic = "/Upload/" + fileName;
//得到原图的宽高
Image image = new Bitmap(filePhysicalPath);
width = image.Width;
height = image.Height;
}
catch (Exception ex)
{
error = ;
}
return Json(new
{
pic = pic,
error = error,
width = width,
height = height
});
}

保存图片

来自http://www.jb51.net/article/33622.htm

1.按照百分比截图

/// <summary>
/// 按照比例缩小图片
/// </summary>
/// <param name="srcImage">要缩小的图片</param>
/// <param name="percent">缩小比例</param>
/// <returns>缩小后的结果</returns>
public static Bitmap PercentImage(Image srcImage, double percent)
{
// 缩小后的高度
int newH = int.Parse(Math.Round(srcImage.Height * percent).ToString());
// 缩小后的宽度
int newW = int.Parse(Math.Round(srcImage.Width * percent).ToString());
try
{
// 要保存到的图片
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.Default;
g.DrawImage(srcImage, new Rectangle(, , newW, newH), new Rectangle(, , srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch (Exception)
{
return null;
}
}

按照百分比截图

2.按照指定像素大小截图

/// <summary>
/// 按照指定大小缩放图片
/// </summary>
/// <param name="srcImage"></param>
/// <param name="iWidth"></param>
/// <param name="iHeight"></param>
/// <returns></returns>
public static Bitmap SizeImage(Image srcImage, int iWidth, int iHeight)
{
try
{
// 要保存到的图片
Bitmap b = new Bitmap(iWidth, iHeight);
Graphics g = Graphics.FromImage(b);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(srcImage, new Rectangle(, , iWidth, iHeight), new Rectangle(, , srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch (Exception)
{
return null;
}
}

按照指定像素大小截图

3.按照指定像素大小截图(但为了保证图片的原始比例,将对图片从中心进行截取,达到图片不被拉伸的效果

/// <summary>
/// 按照指定大小缩放图片,但是为了保证图片宽高比自动截取
/// </summary>
/// <param name="srcImage"></param>
/// <param name="iWidth"></param>
/// <param name="iHeight"></param>
/// <returns></returns>
public static Bitmap SizeImageWithOldPercent(Image srcImage, int iWidth, int iHeight)
{
try
{
// 要截取图片的宽度(临时图片)
int newW = srcImage.Width;
// 要截取图片的高度(临时图片)
int newH = srcImage.Height;
// 截取开始横坐标(临时图片)
int newX = ;
// 截取开始纵坐标(临时图片)
int newY = ;
// 截取比例(临时图片)
double whPercent = ;
whPercent = ((double)iWidth / (double)iHeight) * ((double)srcImage.Height / (double)srcImage.Width);
if (whPercent > )
{
// 当前图片宽度对于要截取比例过大时
newW = int.Parse(Math.Round(srcImage.Width / whPercent).ToString());
}
else if (whPercent < )
{
// 当前图片高度对于要截取比例过大时
newH = int.Parse(Math.Round(srcImage.Height * whPercent).ToString());
}
if (newW != srcImage.Width)
{
// 宽度有变化时,调整开始截取的横坐标
newX = Math.Abs(int.Parse(Math.Round(((double)srcImage.Width - newW) / ).ToString()));
}
else if (newH == srcImage.Height)
{
// 高度有变化时,调整开始截取的纵坐标
newY = Math.Abs(int.Parse(Math.Round(((double)srcImage.Height - (double)newH) / ).ToString()));
}
// 取得符合比例的临时文件
Bitmap cutedImage = CutImage(srcImage, newX, newY, newW, newH);
// 保存到的文件
Bitmap b = new Bitmap(iWidth, iHeight);
Graphics g = Graphics.FromImage(b);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.Default;
g.DrawImage(cutedImage, new Rectangle(, , iWidth, iHeight), new Rectangle(, , cutedImage.Width, cutedImage.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch (Exception)
{
return null;
}
}

按照指定像素大小截图

4.jpeg图片质量压缩,压缩的比例参数在1-100之间。(适量的压缩对于肉眼来说没有什么明显的区别,但是能够大大的减小图片的占用大小
/// <summary>
/// jpeg图片压缩
/// </summary>
/// <param name="sFile"></param>
/// <param name="outPath"></param>
/// <param name="flag"></param>
/// <returns></returns>
public static bool GetPicThumbnail(string sFile, string outPath, int flag)
{
System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;
//以下代码为保存图片时,设置压缩质量
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[];
qy[] = flag;//设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = ; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
if (jpegICIinfo != null)
{
iSource.Save(outPath, jpegICIinfo, ep);//dFile是压缩后的新路径
}
else
{
iSource.Save(outPath, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
iSource.Dispose();
}
}

jpeg图片质量压缩

PS:之上用的CutImage方法的补充

/// <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 CutImage(Image 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);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(, , iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}

CutImage方法的补充

上一篇:蒲公英 Bug 管理云


下一篇:maya2012卸载/安装失败/如何彻底卸载清除干净maya2012注册表和文件的方法