CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

请问如何用GDI+方式创建图片?

楼主cpdp(CP设计动力)2003-06-02 02:45:01 在 .NET技术 / ASP.NET 提问

! 问题点数:100、回复次数:1Top

1 楼szwebnet(赤水流星)回复于 2003-06-02 02:47:35 得分 100

Namespaces   Used:  
   
   
  System    
   
  System.Drawing    
   
  System.Drawing.Text    
   
  System.Drawing.Drawing2D    
   
  System.Drawing.Imaging  
   
      Private   Sub   Page_Load(Sender   as   Object,   E   As   Eventargs)    
                  'First   Lets   Dim   some   variables....    
                  'Width   and   Height   of   the   image   we   are   making    
                  Dim   aWidth   As   Integer   =   300    
                  Dim   aHeight   As   Integer   =   300    
   
                  'Background   color   of   the   image    
                  Dim   aBgColor   As   Drawing.Color   =   Drawing.Color.White    
   
                  'Text   to   "Draw"   on   the   image    
                  Dim   aText   As   String   =   "Hello   World"    
   
                  'Font   and   font   color   to   use    
                  Dim   aFont   As   New   Drawing.Font("Comic   Sans   MS",   18,   Drawing.FontStyle.Regular,   Drawing.GraphicsUnit.Pixel)    
                  Dim   aFontColor   As   Drawing.Color   =   Drawing.Color.Black    
   
                  'Rectangle's   color   (You   can   also   use   Drawing.Color.FromName(String)   to   get   a   "Color"   from   a   string   like   "Gold"    
                  Dim   aRectangleColor   As   Drawing.Color   =   Drawing.Color.FromName("LightGray")    
   
                  'now   lets   start   the   main   work    
                  'We   start   by   Dim'ing   a   new   Bitmap   image   with   our   Width,   Height   and   Image   Format    
                  Dim   b   As   New   Drawing.Bitmap(aWidth,   aHeight,   Drawing.Imaging.PixelFormat.Format24bppRgb)    
   
                  'Next   we   create   a   Graphics   object   from   the   bitmap   image.   This   is   used   for   drawing   with,   kinda   like   a   tool.    
                  Dim   g   As   Drawing.Graphics   =   Drawing.Graphics.FromImage(b)    
   
                  'We   will   clear   the   image   to   the   background   color.    
                  g.Clear(aBgColor)    
   
                  'Set   some   SmoothingMode   so   that   we   arnt   all   pixelated.    
                  g.SmoothingMode   =   Drawing.Drawing2D.SmoothingMode.AntiAlias    
   
                  'StringFormat   is   used   to   align   the   text   in   this   example..   It   has   other   uses   as   well.    
                  Dim   SFormat   As   New   Drawing.StringFormat()    
                  SFormat.Alignment   =   Drawing.StringAlignment.Center    
   
                  'now   the   fun   part.   The   Drawing   !!!    
                  'First   lets   Draw   our   Text   onto   the   bitmap    
                  'This   is   done   via   DrawString(text   As   String,   Font   As   Font,   Brush   As   Brush,   X,   Y,   StringFormat)    
                  'The   SFormat   is   set   to   align   the   text   centered.   It   will   be   centered   on   the   X   position   of   the   draw    
                  'so   we   set   the   X   value   to   half   of   the   width   of   our   image   (b)    
                  g.DrawString(aText,   aFont,   New   Drawing.SolidBrush(aFontColor),   b.Width   /   2,   5,   sFormat)    
   
                  'Next   lets   draw   our   rectangle,   We   use   FillRectangle   to   get   a   solid   one,   and   Drawrectangle   to   get   one   with   borders    
                  'FillRectangle(Brush,   X,   Y,   Width,   Height)    
                  g.FillRectangle(New   Drawing.SolidBrush(aRectangleColor),   10,   100,   b.Width   -   20,   100)    
   
                  'Now   we   are   almost   done...    
                  'We   Clear()   The   Response.   and   Set   the   ContentType   to   Jpeg    
                  Response.Clear()    
                  Response.ContentType   =   "image/jpeg"    
   
                  'Then   we   Save   the   Bitmap   to   the   OutputStream.    
                  'Save(Stream   as   IO.Stream,   Format   as   ImageFormat)    
                  b.Save(Response.OutputStream,   Drawing.Imaging.ImageFormat.Jpeg)    
   
                  'Dispose   of   the   Bitmap,   Flush   the   response,   And   End()    
                  b.Dispose()    
                  Response.Flush()    
                  Response.End()    
                  'Thats   It...    
          End   SubTop

相关问题

  • 请问:怎样用GDI+创建并保存一个图片?
  • 对象创建的方式
  • ActiveX控件中GDI+的Image::FromFile创建图片总是失败,但普通程序中可以,这是为什么?
  • 急!如何创建快捷方式?online
  • 用 SDK 的方式,怎样创建 ToolBar ?
  • 如何创建这个快捷方式?
  • 如何创建桌面快捷方式?
  • vc 如何创建快捷方式呀
  • 怎样创建一个快捷方式
  • 创建快捷方式的问题

关键词

  • jpeg
  • drawing
  • sformat
  • smoothingmode
  • dim
  • bitmap
  • fillrectangle
  • stringformat
  • color
  • imaging

得分解答快速导航

  • 帖主:cpdp
  • szwebnet

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo