zoukankan
html css js c++ java
如何生成图片的缩略图
以下为页面:
要上传的图片
原图片
缩略图
以下为代码:
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
namespace
CommonFunction
{
/**/
///
<summary>
///
smallImg 的摘要说明。
///
</summary>
public
class
smallImg : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.Label Label1;
protected
System.Web.UI.WebControls.Label Label2;
protected
System.Web.UI.WebControls.Label Label3;
protected
System.Web.UI.WebControls.Button btnUp;
protected
System.Web.UI.WebControls.Image imageSource;
protected
System.Web.UI.WebControls.Image imageSmall;
protected
System.Web.UI.HtmlControls.HtmlInputFile upImage;
//
定义image类的对象
System.Drawing.Image image,newimage;
//
图片路径
protected
string
imagePath;
//
图片类型
protected
string
imageType;
//
图片名称
protected
string
imageName;
//
提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行
//
如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true;否则返回 false
System.Drawing.Image.GetThumbnailImageAbort callb
=
null
;
private
void
Page_Load(
object
sender, System.EventArgs e)
{
//
在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码
#region
Web 窗体设计器生成的代码
override
protected
void
OnInit(EventArgs e)
{
//
//
CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base
.OnInit(e);
}
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.btnUp.Click
+=
new
System.EventHandler(
this
.btnUp_Click);
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
}
#endregion
private
void
btnUp_Click(
object
sender, System.EventArgs e)
{
string
mPath;
if
(
""
!=
upImage.PostedFile.FileName)
{
imagePath
=
upImage.PostedFile.FileName;
//
取得图片类型
imageType
=
imagePath.Substring(imagePath.LastIndexOf(
"
.
"
)
+
1
);
//
取得图片名称
imageName
=
imagePath.Substring(imagePath.LastIndexOf(
"
\\
"
)
+
1
);
//
判断是否是JPG或者GIF图片,这里只是举个例子,并不一定必须是这两种图片
if
(
"
jpg
"
!=
imageType
&&
"
gif
"
!=
imageType)
{
Response.Write(
"
<script language='javascript'> alert('对不起!请您选择jpg或者gif格式的图片!');</script>
"
);
return
;
}
else
{
try
{
//
建立虚拟路径
mPath
=
Server.MapPath(
"
upFile
"
);
//
保存到虚拟路径
upImage.PostedFile.SaveAs(mPath
+
"
\\
"
+
imageName);
//
显示原图
imageSource.ImageUrl
=
"
upFile/
"
+
imageName;
//
为上传的图片建立引用
image
=
System.Drawing.Image.FromFile(mPath
+
"
\\
"
+
imageName);
//
生成缩略图
newimage
=
image.GetThumbnailImage(
100
,
100
,callb,
new
System.IntPtr());
//
把缩略图保存到指定的虚拟路径
newimage.Save(Server.MapPath(
"
upFile
"
)
+
"
\\small
"
+
imageName);
//
释放image对象占用的资源
image.Dispose();
//
释放newimage对象的资源
newimage.Dispose();
//
显示缩略图
imageSmall.ImageUrl
=
"
upFile/
"
+
"
small
"
+
imageName;
Response.Write(
"
上传成功!
"
);
}
catch
{
Response.Write(
"
上传成功!
"
);
}
}
}
}
}
}
查看全文
相关阅读:
SSH框架搭建全过程详解
SpringMVC项目配置全过程详解
<抽奖奇遇>
extJS--尚
CentOS 7.3安装指南
javaWeb项目之图书管理系统(附视频讲解)
Swing记事本项目
五、PTA实验作业(结构体)
四、PTA实验作业(指针)
三、PTA实验作业(数组)
原文地址:https://www.cnblogs.com/qingyang/p/139928.html
最新文章
深入浅出核函数
偏度与峰度的正态性分布判断
怎么用Q-Q图验证数据集的分布
机器学习概论
Linux环境下创建KVM虚机
Error: Cannot find module 'webpack-cli/bin/config-yargs' webpack编译时遇到的 npm run dev 报错问题
机器学习-逻辑回归
关于var和ES6中的let,const的理解
小程序模板消息使用
小程序之生成朋友圈图片
热门文章
小程序navigateTo和redirectTo的使用
小程序学习之快递查询(一)
论文笔记 2016-CIKM Learning Graph-based POI Embedding for Location-based Recommendation
我用Python抓取了自如上所有的租房信息,随心所欲的选房
我用Python爬取网易云音乐上的Hip-hop歌单,分析rapper如何押韵
简单实现Arraylist和Linkedlist(一)
快速排序算法分析
如何自定义一个长度可变数组
HDOJ(~1004)
[JSOI2008]星球大战
Copyright © 2011-2022 走看看