使用ASP.NET动态生成图片



using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Birdshome
{
    
/// <summary>
    
/// Summary description for _Default.
    
/// </summary>

    public class _Default : System.Web.UI.Page
    
{
        
int m_Unit = 3;

        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// Put user code to initialize the page here
            int counter_width = 32/8;
            
int counter_height =16;
            Bitmap bmp 
= new Bitmap(counter_width*m_Unit*8, counter_height*m_Unit);
            GenerateBitmap(bmp);
            Response.Clear();
            Response.ContentType 
= "image/jpeg";
               bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
        }


        
Web Form Designer generated code

        
private void GenerateBitmap(Bitmap bmp)
        
{
            
//#define counter_width 32
            
//#define counter_height 16
            int counter_width = 32/8;
            
int counter_height =16;
            
byte [] counter_bits = new byte[]
            
{
                
0x000x000x000x00,  0x000x000x000x00
                
0x000x000x000x00,  0x000x000x000x00
                
0x000x000x000x00,  0x000x000x000x00
                
0x3E0x1C0x7F0x7F,  0x630x060x030x03
                
0x600x030x030x03,  0x600x030x030x03
                
0x3C0x3F0x3F0x3F,  0x600x630x600x60
                
0x600x630x600x60,  0x600x630x600x60
                
0x630x630x630x63,  0x3E0x3E0x3E0x3E
            }
;
            Graphics g 
= Graphics.FromImage(bmp);
            
forint i=0 ; i < counter_height ; i++ )
            
{
                
forint j=0 ; j < counter_width ; j++ )
                
{
                    
byte by = counter_bits[counter_width*i+j];
                    
forint k=0 ; k < 8 ; k++ )
                    
{
                        
byte bit = 0;
                        bit 
= (byte)(by & 0x01);
                        Rectangle rect 
= new Rectangle(k*m_Unit+j*8*m_Unit, i*m_Unit, m_Unit, m_Unit);
                        Brush brush;

                        
if( bit == 0x01 )
                        
{
                            brush 
= Brushes.Yellow;
                        }

                        
else
                        
{
                            brush 
= Brushes.Blue;
                        }

                        g.FillRectangle(brush, rect);
                        by 
= (byte)(by >> 1);
                    }

                }

            }

        }

    }

}

posted on 2004-08-29 21:39  birdshome  阅读(3620)  评论(1编辑  收藏  举报

导航