2004年10月10日

[java]内存中生成的图片显示到页面中

 

下载:点击下载
演示:本站暂时不支持JSP空间


Imaging_ToWeb.jsp

<%@ page autoFlush="false"  import="java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*"%>
<%
    String ImageStr = request.getParameter("ID_Text");
    if(ImageStr==null || ImageStr.equals(""))
    {
        response.setContentType("text/html; charset=gb2312");
 %>
<HTML>
 <HEAD>
  <title>Http://www.lionsky.net</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 </HEAD>
 <body>
  <form id="Form1" method="post">
    <input type="text" id="ID_Text" name=ID_Text>
    <input type=submit value="GO">
  </form>
 </body>
</HTML>

<%
       }
       else
       {
        out.clear();
        response.setContentType("image/jpeg");
        response.addHeader("pragma","NO-cache");
        response.addHeader("Cache-Control","no-cache");
        response.addDateHeader("Expries",0);
        int width=300, height=100;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        //以下填充背景颜色
        g.setColor(Color.lightGray);
        g.fillRect(0, 0, width, height);
        String random="random";
        //以下设置前景色
        g.setColor(Color.BLACK);
        g.drawString("Http://www.lionsky.net",10,20);
        g.drawString("Author:Lion[lion-a@sohu.com]",10,40);
        g.drawLine(10,50,290,50);
        g.drawString(ImageStr,10,70);
        g.dispose();
        ServletOutputStream outStream = response.getOutputStream();
        JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream);
        encoder.encode(image);
        outStream.close();
    }

%>

posted @ 2004-10-10 10:10 Lion 阅读(1285) 评论(0) 编辑

[C#]在内存中生成的图片显示到页面中

 

下载:点击下载
演示:点击演示

System.Drawing.Imaging_ToWeb.aspx
<%@ Page language="c#" Codebehind="System.Drawing.Imaging_ToWeb.aspx.cs" Src="System.Drawing.Imaging_ToWeb.aspx.cs" AutoEventWireup="false" Inherits="Exam.Image_ToWeb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>Lion互动网络--在内存中生成的图片显示到页面中</title>
 </HEAD>
 <body MS_POSITIONING="FlowLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:TextBox id="ID_Text" runat="server"></asp:TextBox>
    <asp:Button id="ID_ShowImage" OnClick="ID_ShowImage_Click" runat="server" Text="显示成图片"></asp:Button></FONT>
  </form>
 </body>
</HTML>


System.Drawing.Imaging_ToWeb.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using  System.Drawing.Drawing2D; 
using  System.Drawing.Imaging; 

namespace Exam
{
 /// <summary>
 /// WebForm2 的摘要说明。
 /// </summary>
 public class Image_ToWeb : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button ID_ShowImage;
  protected System.Web.UI.WebControls.TextBox ID_Text;
 
  
  protected void ID_ShowImage_Click(object sender, System.EventArgs e)
  {
   if(ID_Text.Text.Trim()!=string.Empty)
   {
    Response.Clear(); 
    int  height=100; 
    int  width=300; 
 
    Bitmap  bmp=new  Bitmap(width,height,PixelFormat.Format24bppRgb); 
    Graphics g=Graphics.FromImage(bmp); 
 
    g.SmoothingMode=SmoothingMode.AntiAlias; 
    g.Clear(Color.LightGray); 
    g.DrawRectangle(Pens.WhiteSmoke,0,0,width,height); 
    g.DrawString(ID_Text.Text.Trim(),new  Font("Arial",10),SystemBrushes.WindowText,new  PointF(10,60)); 
                                                
    bmp.Save(this.Response.OutputStream,ImageFormat.Gif); 


    g.Dispose(); 
    bmp.Dispose(); 
    Response.End();
   }
  }
 }
}

posted @ 2004-10-10 10:09 Lion 阅读(1716) 评论(4) 编辑