简单的保存数据流

<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方法的补充

c#截取图片的更多相关文章

  1. node上截取图片工具 images(node-images)

    我们经常会遇到服务器上传的图片进行裁剪或者增加logo等等一些操作,在node平台上该如何实现呢? 看到大家都在使用"gm"这个工具,功能很强大,但是在Windows平台上简直就是 ...

  2. IOS 截取图片 部分 并生成新图片

    /** * 从图片中按指定的位置大小截取图片的一部分 * * @param image UIImage image 原始的图片 * @param rect CGRect rect 要截取的区域 * * ...

  3. iOS 根据UIImage 修改UIImageView Frame (包括截取图片中间部分)

    iOS UIImageView 根据需求调整frame 1.图片的宽和高不相等,截取图片的中间部分,截取的部分Size明确 2.图片的宽度要等于其父视图的类的宽度,然后根据宽度计算高度,保证 图片不变 ...

  4. [ATL/WTL]_[0基础]_[CBitmap复制图片-截取图片-平铺图片]

    场景: 1.当你须要截取图片部分区域作为某个控件的背景. 2.须要平铺图片到一个大区域让他自己主动放大时. 3.或者须要合并图片时. 代码: CDC sdc; CDC ddc; sdc.CreateC ...

  5. Unity 截取图片并且显示出来

    Unity 截取图片并且显示出来 近期用到了unity的截图,并且把他显示出来,摸索了很多! 讲解一个东西,android可以存储一些文件在本地,比如配置文件等! 对于不同的系统,方法不一! if ( ...

  6. C# 截取图片区域,并返回所截取的图片

    /// <summary> /// 截取图片区域,返回所截取的图片 /// </summary> /// <param name="SrcImage" ...

  7. DDGScreenShot—截取图片的任意部分

    写在前面 DDGScreenShot 库提供了截取任意图片的功能, 支持手势截图,当然,输入任意的区域也可以,下面看看具体的代码 代码如下: 方法封装 /** ** 用手势截图(截取图片的任意部分) ...

  8. 机器学习进阶-图片基本处理-ROI区域 1.img[0:200, 0:200]截取图片 2.cv2.split(对图片的颜色通道进行拆分) 3. cv2.merge(将颜色通道进行合并) 4 cur_img[:, :, 0] = 0 使得b通道的颜色数值为0

    1. 截取图片的部分区域img[0:200, 0:200], 读入的图片是ndarray格式 2. b, g, r = cv2.split(img)  # 对图片的颜色通道进行拆分 3.img = c ...

  9. 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示

    我们都知道在ios中,每一个UIImageView都有他的frame大小,但是如果图片的大小和这个frame的大小不符合的时候会怎么样呢?在默认情况,图片会被压缩或者拉伸以填满整个区域. 通过查看UI ...

随机推荐

  1. vim自动跳转到引用的函数

        安装:  yum install ctags 在你代码的根目录下执行:比如/data/www/test/trunkctags -R * 打开文件只能在根目录下打开就可以,比如 vim appl ...

  2. 在WMware新建一个虚拟机

  3. C语言 · 输出日历

    算法提高 输出日历   时间限制:1.0s   内存限制:512.0MB      按照下述格式打印2006年12月日历: Calendar 2006-12---------------------- ...

  4. 7 款灵巧实用的 CSS3/jQuery 工具

    作为 Web 前端开发者,应该对 jQuery 比较熟悉,对免费开源的 jQuery 也用的非常多.但是随着 CSS3 标准的诞生和发展,很多 jQuery 插件也都纷纷应用了 CSS3 新标准,也因 ...

  5. 关于PHP开发所需要的工具和环境

    0.notepad++ 一个类型记事本的软件,用来看安装的部署说明命令. 1.虚拟机 在虚拟机里面操作,本机不会被影响. 2.CentOS系统 类似Linux的系统,在里面安装PHP,Nginx,ph ...

  6. 有2个表,结构相似,有一个字段关联,现在怎么把A表的数据添加到B表中,条件是A表不在B表的数据?? 请各位高手多多指点,是oracle的数据库

    : insert into b(col1,col2 )select col1,col2 where not exists(select a.col1 from a where a.col1 = b.c ...

  7. Spring 4 官方文档学习(五)核心技术之SpEL

    题外话 官方文档用evaluate这个单词来描述从表达式中获得实际内容的过程.如果直译的话,应该是评估.估值之类的意思.个人以为翻译成解析更易懂,但parse已经是解析了,为了避免冲突,就只好保留了e ...

  8. e671. 在缓冲图像中存取像素

    // Get a pixel int rgb = bufferedImage.getRGB(x, y); // Get all the pixels int w = bufferedImage.get ...

  9. The request lifetime scope cannot be created because the HttpContext is not available

    项目中应用了Autofac,在Global轮询处理Job的时候,需要获取现有得Service,而这些Service已经通过Autofac进行了配置,所以理所应当的用下面的代码去获取了. Depende ...

  10. (转)sqlite3使用中的常见问题

    1. 创建数据如果不往数据库里面添加任何的表,这个数据库等于没有建立,不会在硬盘上产生任何文件,如果数据库已经存在,则会打开这个数据库. 2. 如何通过sqlite3.dll与sqlite3.def生 ...