2014-02-10 73 views
3

我得到了使用C#將文本轉換爲圖像的代碼。代碼如下。 現在我的排隊是這個函數返回一個位圖圖像。如何在我的asp.net頁面中顯示它。我想顯示這個函數返回的圖像。將文本轉換爲圖像

private Bitmap CreateBitmapImage(string sImageText) 
{ 
    Bitmap objBmpImage = new Bitmap(1, 1); 

    int intWidth = 0; 
    int intHeight = 0; 

    // Create the Font object for the image text drawing. 
    Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel); 

    // Create a graphics object to measure the text's width and height. 
    Graphics objGraphics = Graphics.FromImage(objBmpImage); 

    // This is where the bitmap size is determined. 
    intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width; 
    intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height; 

    // Create the bmpImage again with the correct size for the text and font. 
    objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight)); 

    // Add the colors to the new bitmap. 
    objGraphics = Graphics.FromImage(objBmpImage); 

    // Set Background color 
    objGraphics.Clear(Color.White); 
    objGraphics.SmoothingMode = SmoothingMode.AntiAlias; 
    objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias; 
    objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0); 
    objGraphics.Flush(); 
    return (objBmpImage); 
} 

回答

2

有此用下面的函數

public Bitmap ConvertTextToImage(string txt, string fontname, int fontsize, Color bgcolor, Color fcolor, int width, int Height) 
    { 
     Bitmap bmp = new Bitmap(width, Height); 
     using (Graphics graphics = Graphics.FromImage(bmp)) 
     { 

      Font font = new Font(fontname, fontsize); 
      graphics.FillRectangle(new SolidBrush(bgcolor), 0, 0, bmp.Width, bmp.Height); 
      graphics.DrawString(txt, font, new SolidBrush(fcolor), 0, 0); 
      graphics.Flush(); 
      font.Dispose(); 
      graphics.Dispose(); 


     } 
     return bmp; 
    } 

,並使用該函數的示例:

ConvertTextToImage(txtvalue.Text, "Bookman Old Style", 10, Color.Yellow, Color.Red, txtvalue.Width, txtvalue.Height); 
+0

如何存儲返回值? – Sonali

+0

只需在代碼背後提供源代碼。 – 2014-02-10 10:58:04

+0

我已經在asp.nt中採取了圖像控制頁面 – Sonali

2

創建HttpHandler(像image.ashx)和使用這樣的代碼:

public void ProcessRequest(HttpContext context) 
    { 
     context.Response.ContentType = "image/png"; 
     var image = CreateBitmapImage("Hello world"); 
     var ms = new MemoryStream(); 
     image.Save(ms, ImageFormat.Png); 

     context.Response.BinaryWrite(ms.ToArray()); 
    } 

並且您需要在圖像中添加鏈接。

<img src="image.ashx" alt="image" /> 

更新[基於評論]:

HTTP處理程序:

public void ProcessRequest(HttpContext context) 
    { 
     context.Response.ContentType = "image/png"; 

     var text = context.Request.Params["text"]; 
     if (text == null) text = string.Empty; 
     var image = CreateBitmapImage(text); 

     image.Save(context.Response.OutputStream, ImageFormat.Png); 
    } 

頁面佈局: -

<asp:TextBox runat="server" ID="MyTextBox"></asp:TextBox> 
    <asp:Button runat="server" OnClick="RenderImageButtonClicked" Text="Change text"/> 
    <asp:Image runat="server" ID="MyTextImage" /> 

頁事件: -

protected void RenderImageButtonClicked(object sender, EventArgs e) 
    { 
     MyTextImage.ImageUrl = "CreateImageText.ashx?text=" + HttpContext.Current.Server.UrlEncode(MyTextBox.Text); 
    } 
+0

如何在我的.ashx文件中訪問這個函數CreateBitmapImage()? 我在.aspx.cs頁面中寫了這個函數嗎? – Sonali

+0

將您的CreateBitmapImage方法的代碼移動到單獨的類或此處理程序中。 –

+0

Okey.but然後如何在這個函數中傳遞一個文本框的值CreateBitmapImage() 我想CreateBitmapImage(Textbox1.text);而不是 CreateBitmapImage(「Hello world」); – Sonali

0

爲了在您的頁面上顯示圖像,您需要在HTML中創建一個IMG標記。您可以手動執行此操作或使用ASP.NET的圖像控件。 IMG標籤包含src-attribute中圖像的地址。這可能是一個靜態文件引用,或者像你的情況那樣是對動態創建的映像的引用。當瀏覽器顯示HTML文件時,它會查找IMG標籤並檢索src-attribute中引用的內容。
顯示動態創建圖像的簡單方法是創建一個將圖像傳輸到客戶端的特殊頁面。在頁面(如在Page_Load中),你可以使用Save - 方法的位圖的將它寫入響應流:

var bm = CreateBitmapImage("MyText"); 
bm.Save(Response.OutputStream, ImageFormat.Jpeg); 
Response.ContentType = "image/jpeg"; 
Response.Flush(); 
Response.End(); 

當我從評論看,該CreateBitmapImage方法位於代碼隱藏頁 - 大概是您要在其上顯示圖像的頁面。爲了在您動態創建圖像時調用該方法,請將代碼移動到新頁面(無論如何您都不需要它)。

在您的IMG標記中,鏈接到新創建的頁面,以便瀏覽器請求圖像。如果你需要爲了一些數據來創建圖像,您需要提供這些到圖像頁面的查詢參數,使你的情況IMG標籤看起來可能是這樣:

<img src="dynamicimage.aspx?text=Mytext" /> 

請注意,使用@Anton描述的處理程序是一種更優雅的方法。但主要構建模塊是相同的。

+0

如何在Handler.ashx中訪問CreateBitmapImage()? – Sonali

+0

@Sonali:您可能需要添加一個名稱空間或創建包含CreateBitmapImage方法的類的實例。 – Markus

0
string text = txtText.Text.Trim(); 
Bitmap bitmap = new Bitmap(1, 1); 
Font font = new Font("Arial", 25, FontStyle.Regular, GraphicsUnit.Pixel); 
Graphics graphics = Graphics.FromImage(bitmap); 
int width = (int)graphics.MeasureString(text, font).Width; 
int height = (int)graphics.MeasureString(text, font).Height; 
bitmap = new Bitmap(bitmap, new Size(width, height)); 
graphics = Graphics.FromImage(bitmap); 
graphics.Clear(Color.White); 
graphics.SmoothingMode = SmoothingMode.AntiAlias; 
graphics.TextRenderingHint = TextRenderingHint.AntiAlias; 
graphics.DrawString(text, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0); 
graphics.Flush(); 
graphics.Dispose(); 
string fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".jpg"; 
bitmap.Save(Server.MapPath("~/images/") + fileName, ImageFormat.Jpeg); 
imgText.ImageUrl = "~/images/" + fileName; 
imgText.Visible = true;