首页 教程 游戏 小说 论坛 搜索
posts - 96, comments - 99, trackbacks - 2, articles - 1
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

如何在上传的图片上写字

Posted on 2007-12-11 16:11 热情的沙漠 阅读(173) 评论(0)  编辑 收藏 网摘 所属分类: C#

很多时候需要在用户上传的图片上加上版权或者一些其他的附加文字信息,如何实现这样的功能,下面帖个简单实现的例子,起到抛砖引玉的作用。
<%@ Page Language="c#" Debug="true" Trace="true"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<html>
<script runat =server>
   void UploadBtn_Click(Object sender, EventArgs e) {
    String filename;
    String filename1;
    String[] filename2;
    int q;
    filename=UploadFile.PostedFile.FileName ;
    filename2=filename.Split(new Char[] {'\\'});
    q=filename2.GetUpperBound(0);
    filename1=filename2[q];
    dis.Text="上传文件名:"+filename1+"<br>";
    UploadFile.PostedFile.SaveAs(Server.MapPath(filename1));
    ImageEditor.Visible = true;
    dis.Text+="文件大小:"+UploadFile.PostedFile.ContentLength+"字节数";
    Image1.Src=filename1;   
   }
   void UpdateBtn_Click(Object sender, EventArgs e) {
    String filename1;
    filename1=Image1.Src;
    System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(filename1));
    System.Drawing.Image newimage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRGB);
    Graphics g = Graphics.FromImage(newimage);
    g.DrawImage(image,0,0,image.Width,image.Height);
    Font f = new Font(FontType.SelectedItem.Text, Int32.Parse(FontSize.SelectedItem.Text));
    Brush b = new SolidBrush(Color.Red);
    g.DrawString(Caption.Text, f, b, 10, 140);
    g.Dispose();
    System.Drawing.Image thumbImage = newimage.GetThumbnailImage(Int32.Parse(Width.Text),Int32.Parse
(Height.Text),null,0);
    image.Dispose();
    thumbImage.Save(Server.MapPath(filename1), ImageFormat.JPEG);
    Image1.Src=filename1;   
    Caption.Text="";  
 
   }

 </script>

 <body>
 <asp:label id="dis" runat=server/>
  <form enctype="multipart/form-data" runat=server>
     选择上传文件: <input id="UploadFile" type=file runat=server>
     <asp:button Text="Upload Me!" OnClick="UploadBtn_Click" runat=server/>
     <hr>
     <asp:panel id="ImageEditor" Visible=false runat=server>
      <img ID="Image1" src="" runat="server"/>
        图像宽度: <asp:textbox id="Width" runat=server/>
        图像高度: <asp:textbox id="Height" runat=server/> <br>
        文本标题: <asp:textbox id="Caption" runat=server/>
        标题字号: <asp:dropdownlist id="FontSize" runat=server>
                    <asp:listitem>14</asp:listitem>
                    <asp:listitem>18</asp:listitem>
                    <asp:listitem>26</asp:listitem>
                    <asp:listitem>36</asp:listitem>
                    <asp:listitem>48</asp:listitem>
                    <asp:listitem>62</asp:listitem>
                   </asp:dropdownlist>
        标题字体: <asp:dropdownlist id="FontType" runat=server>
                    <asp:listitem>黑体</asp:listitem>
                    <asp:listitem>仿宋</asp:listitem>
                    <asp:listitem>隶书</asp:listitem>
                    <asp:listitem>楷书</asp:listitem>
                    <asp:listitem>彩云</asp:listitem>
                    <asp:listitem>新魏</asp:listitem>
                   </asp:dropdownlist>
                        
        <asp:button Text="Update Image" OnClick="UpdateBtn_Click" runat=server/>
     </asp:panel>
   </form>

 </body>
</html>


我的网站:www.99New.com.cn

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-12-11 16:17 编辑过
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》



相关文章:

相关链接:
 
.