用程式碼產生圖片是很常用的需求,

這裡教你怎麼把版權宣告文字PRINT在圖片上,

再用IMG CONTROL把它抓出來, 然後不留垃圾在Server.

”Defautlt.aspx

<%@ Page Language=”VB” AutoEventWireup=”false” CodeFile=”Default.aspx.vb” Inherits=”_Default” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
    <title>Untitled Page</title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
         <!– 用IMAGECONTROL把它抓出來 –>
         <asp:Image ID=”Image1″ runat=”server” ImageUrl=”~/getImage.aspx” />
    </div>
    </form>
</body>
</html>

” getImage.aspx

Imports System.Drawing
Imports System.Drawing.Imaging

Partial Class getImage
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim s As String = “NewtonStudio”
       ‘若不要固定圖片路徑, 可以用querystring來抓.
       ‘Dim bmpFile As String = Server.MapPath(Request.QueryString(“id”).ToString())
        Dim bmpFile As String = Server.MapPath(“ADONETx.jpg”)
        Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(bmpFile)
        Dim g As Graphics = Graphics.FromImage(img)
        Dim f As New Font(“Courier New”, 10)
        Dim b As New SolidBrush(Color.Black)
        g.DrawString(s, f, b, 0, 0)
        img.Save(Response.OutputStream, ImageFormat.Jpeg)

        img.Dispose()
    End Sub
End Class

 

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

required