zoukankan
html css js c++ java
GDI+中对图片的裁剪
CutImage
1
void
CutImage(HttpPostedFile post,
string
ppuid,
out
string
imagename)
2
{
3
System.Drawing.Image SourceImg
=
System.Drawing.Image.FromStream(post.InputStream);
4
if
(SourceImg.Height
>
ConfigHelper.UserFaceMaxHeight)
5
{
6
this
._lbl_upload_msg.Text
=
"
最大高度不得大于
"
+
ConfigHelper.UserFaceMaxHeight;
7
return
;
8
}
9
if
(SourceImg.Width
>
ConfigHelper.UserFaceMaxWidth)
10
{
11
this
._lbl_upload_msg.Text
=
"
最大宽度不得大于
"
+
ConfigHelper.UserFaceMaxWidth;
12
return
;
13
}
14
ImageFormat format
=
getImageformat(System.IO.Path.GetExtension(post.FileName));
15
string
filename
=
ppuid
+
"
.
"
+
format.ToString();
16
imagename
=
filename;
17
18
if
(
!
UserFaceDir.EndsWith(
"
\\
"
))
19
UserFaceDir
=
UserFaceDir
+
"
\\
"
;
20
filename
=
UserFaceDir
+
filename;
21
int
SourceImgWidth
=
SourceImg.Width;
22
int
SourceImgHeight
=
SourceImg.Height;
23
if
((SourceImgWidth
!=
ConfigHelper.UserFaceWidth)
&&
(SourceImgHeight
!=
ConfigHelper.UserFaceHeight))
24
{
25
//
如果宽高比例为1:1,则直接构成缩略图
26
if
(((Double)SourceImgWidth
/
SourceImgHeight)
==
1
)
27
{
28
System.Drawing.Image thumbimg
=
SourceImg.GetThumbnailImage(ConfigHelper.UserFaceWidth, ConfigHelper.UserFaceHeight,
null
, IntPtr.Zero);
29
thumbimg.Save(filename, format);
30
thumbimg.Dispose();
31
SourceImg.Dispose();
32
return
;
33
}
34
Bitmap bit
=
new
Bitmap(SourceImg);
35
Rectangle rec
=
new
Rectangle();
//
构造一个Rectangle类,一个矩形
36
rec.Width
=
ConfigHelper.UserFaceWidth;
37
rec.Height
=
ConfigHelper.UserFaceHeight;
38
if
(SourceImgWidth
>
rec.Width)
39
rec.X
=
(SourceImgWidth
-
rec.Width)
/
2
;
40
else
41
{
42
rec.X
=
0
;
43
rec.Width
=
SourceImg.Width;
44
}
45
if
(SourceImgHeight
>
rec.Height)
46
rec.Y
=
(SourceImgHeight
-
rec.Height)
/
2
;
47
else
48
{
49
rec.Y
=
0
;
50
rec.Height
=
SourceImg.Height;
51
}
52
53
try
54
{
55
//
这里就是把从上传过程中构造的bitmap克隆一份,并按定义好的矩形裁剪
56
bit.Clone(rec, PixelFormat.DontCare).Save(filename, format);
57
}
58
catch
(Exception ex)
59
{
60
this
._lbl_upload_msg.Text
=
ex.Message;
61
return
;
62
}
63
finally
64
{
65
bit.Dispose();
66
SourceImg.Dispose();
67
}
68
}
69
}
70
查看全文
相关阅读:
如何在原生微信小程序中实现数据双向绑定
【推荐】开源项目minapp-重新定义微信小程序的开发
iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 规范与部署
iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 错误处理
系列3|走进Node.js之多进程模型
手把手教你撸一个 Webpack Loader
iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 记录日志
React Native 网络层分析
如何实现VM框架中的数据绑定
iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 解析JSON
原文地址:https://www.cnblogs.com/0000/p/1611248.html
最新文章
EF连接mysql,出现A call to SSPI failed错误,解决办法
jquery validate 动态生成的多个同名input的验证
C# WebBrowser控件 下载文件不弹下载提示框的办法
C# HttpClient设置cookies的两种办法
从二维数据转换成 JS 树形,JS树形转换成 二维数据
.NET CORE 部署到 CentOS7全部过程
C#/.NET 任务调度器
RabbitMQ学习(6) (远程过程调用(RPC))
RabbitMQ学习 (管理插件)
RabbitMQ学习(5) (主题)
热门文章
RabbitMQ学习(4)(路由)
RabbitMQ学习(2)(工作队列)
RabbitMQ学习(3) (发布/订阅)
RabbitMQ学习(1)(介绍)
iKcamp新书上市《Koa与Node.js开发实战》
React 深入系列5:事件处理
React 深入系列4:组件的生命周期
React 深入系列3:Props 和 State
React 深入系列2:组件分类
React 深入系列1:React 中的元素、组件、实例和节点
Copyright © 2011-2022 走看看