惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - 王朝东

asp.net 实现微信公众平台的主动推送信息 微信公众平台定制开发 音视频程序定制开发 联华ok卡自动并卡程序 企业邮件管理解决方案 电信自动并卡软件 c# tooltip 使用 基于(ICSharpCode.SharpZipLib.dll)的文件压缩方法,类文件 日期格式转换--年 月 周数 日 - 王朝东 不同数据库数据导入方法 Asp.net弹出层并且有遮罩层 OCX制作CAB Asp.net中用户自定义控件 ascx的使用 C#.net ListView item 拖动排序实现方法 对操作系统一些常用模块调用的简单总结 - 王朝东 - 博客园 水晶报表连续打印 好长时间没上了,呵呵!最近在学习Web Service,顺便写个简单的体会吧 使用sql语句实现跨数据库获取信息 如何将数据导入word,excel,文本文件的类
asp.net中生成缩略图,并在图片上加入图片版板等信息
王朝东 · 2007-09-10 · via 博客园 - 王朝东


 
//定义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();
  }