当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net中生成缩略图并添加版权实例代码

asp.net中生成缩略图并添加版权实例代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

绯闻女孩第三季下载,屠龙帮,常州装潢公司

复制代码 代码如下:

//定义image类的对象
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 sm_click(object sender, system.eventargs e)
{
string mpath;

if("" != file1.postedfile.filename) //file1为上传文件控件
{
imagepath = file1.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("uploadfiles");
//保存到虚拟路径
file1.postedfile.saveas(mpath+"\\"+imagename);

//显示原图, imagesource为图片控件
//imagesource.imageurl = "uploadfiles/"+imagename;

//为上传的图片建立引用
image=system.drawing.image.fromfile(mpath+"\\"+imagename);
//生成缩略图
newimage=image.getthumbnailimage(200,200,callb,new system.intptr());
//把缩略图保存到指定的虚拟路径
newimage.save(server.mappath("uploadfiles")+"\\small"+imagename);
//释放image对象占用的资源
image.dispose();
//释放newimage对象的资源
newimage.dispose();
//显示缩略图

addtexttoimg ("uploadfiles/"+"small"+imagename,"pic info"); // 在图片上加入信息说明
image1.imageurl = "uploadfiles/"+"small"+imagename;

script.alert("上传成功!");
}
catch
{
script.alert("上传失败!");
}

} // end else
}

// 在图片上加入自己的信息,
// addtexttoimg (physicpath,"pic info");
private void addtexttoimg(string filename,string text)
{
//string sss = mappath(filename);

if ( !file.exists ( filename)) {
throw new filenotfoundexception("the file don't exist!");
}

//还需要判断文件类型是否为图像类型,这里就不赘述了

system.drawing.image image = system.drawing.image.fromfile(filename);//mappath(filename));
bitmap bitmap = new bitmap(image,image.width,image.height);
graphics g = graphics.fromimage(bitmap);

float fontsize = 22.0f; //字体大小
float textwidth = text.length*fontsize; //文本的长度
//下面定义一个矩形区域,以后在这个矩形里画上白底黑字
float rectx = 0;
float recty = 0;
float rectwidth = text.length*(fontsize+18);
float rectheight = fontsize+18;
//声明矩形域
rectanglef textarea = new rectanglef(rectx,recty,rectwidth,rectheight);
font font = new font("宋体",fontsize);//定义字体
brush whitebrush = new solidbrush(color.white);
brush blackbrush = new solidbrush(color.black);
g.fillrectangle(blackbrush,rectx,recty,rectwidth,rectheight);
g.drawstring(text,font,whitebrush,textarea);
memorystream ms = new memorystream();
//保存为jpg类型
bitmap.save(ms,imageformat.jpeg);

//输出处理后的图像,这里为了演示方便,我将图片显示在页面中了
/**//* response.clear();
response.contenttype = "image/jpeg";
response.binarywrite( ms.toarray() );
*/
filestream fs=new filestream(filename, filemode.openorcreate);//.createnew);
fs.write(ms.toarray(),0,ms.toarray().length);
fs.close();

image1.imageurl = filename; // 将图片显示在image控件中
g.dispose();
bitmap.dispose();
image.dispose();
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网