熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#中使用GDI 讓網站新聞標題個性化

2022-06-13   來源: .NET編程 

  新華網上的今日頭條的標題是用圖片體現的這種頭條我們一般的做法可能是使用Photoshop制作成圖片保存上傳這樣需要浪費人力比較麻煩有沒有更好的辦法呢?

  下面使用GDI+及C#代碼完成自動實現的過程

  老規矩先看看運行效果

  下面是C#代碼

  

  

  // TextImageaspx
// TextImageaspxcs
using System;
using SystemData;
using SystemConfiguration;
using SystemCollections;
using SystemWeb;
using SystemWebSecurity;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIWebControlsWebParts;
using SystemWebUIHtmlControls;
using SystemDrawing;
using SystemDrawingImaging;
using SystemDrawingDrawingD;
using SystemIO;

  namespace BrawDrawComUtility
{
public partial class Utility_TextImage : SystemWebUIPage
{
int _width = ;
public int Width
{
get
{
return _width;
}
set
{
_width = value;
}
}

  int _height = ;
public int Height
{
get
{
return _height;
}
set
{
_height = value;
}
}

  string _text = stringEmpty;
public string Text
{
get
{
return _text;
}
set
{
_text = value;
}
}

  string _fontName = 宋體;
public string FontName
{
get
{
return _fontName;
}
set
{
_fontName = value;
}
}

  Color _fontColor = ColorBlack;
public Color FontColor
{
get
{
return _fontColor;
}
set
{
_fontColor = value;
}
}

  Color _backgroundColor = ColorWhite;
public Color BackgroundColor
{
get
{
return _backgroundColor;
}
set
{
_backgroundColor = value;
}
}

  //華文新魏
protected void Page_Load(object sender EventArgs e)
{
if (RequestQueryString[w] != null)
{
try
{
this_width = intParse(RequestQueryString[w]Trim());
}
finally
{
}
}

  if (RequestQueryString[h] != null)
{
try
{
this_height = intParse(RequestQueryString[h]Trim());
}
finally
{
}
}

  if (RequestQueryString[text] != null)
{
this_text = RequestQueryString[text]Trim();
}
else if (RequestQueryString[t] != null)
{
this_text = RequestQueryString[t]Trim();
}

  if (RequestQueryString[font] != null)
{
this_fontName = RequestQueryString[font]Trim();
}
else if (RequestQueryString[f] != null)
{
this_fontName = RequestQueryString[f]Trim();
}
if (this_fontName == 大黑)
{
this_fontName = 方正大黑簡體;
}
string colorString = Black;
if (RequestQueryString[color] != null)
{
colorString = RequestQueryString[color]Trim();
}
else if (RequestQueryString[c] != null)
{
colorString = RequestQueryString[c]Trim();
}
if (colorStringStartsWith(_))
{
colorString = # + colorStringRemove( );
}
this_fontColor = ConvertColor(colorString);

  string bgColorString = White;
if (RequestQueryString[bgcolor] != null)
{
bgColorString = RequestQueryString[bgcolor]Trim();
}
else if (RequestQueryString[b] != null)
{
bgColorString = RequestQueryString[b]Trim();
}
if (bgColorStringStartsWith(_))
{
bgColorString = # + bgColorStringRemove( );
}

  this_backgroundColor = ConvertColor(bgColorString);

  if (!IsPostBack)
{
CreateImage(thisText thisWidth thisHeight thisFontName thisFontColor this

  BackgroundColor);
}
}

  //根據驗證字符串生成最終圖象
public void CreateImage(string text int width int height string fontName Color

  fontColor Color backgroundColor)
{
Bitmap theBitmap = new Bitmap(width height);
Graphics theGraphics = GraphicsFromImage(theBitmap);
theGraphicsInterpolationMode = SystemDrawingDrawingDInterpolationModeHighQualityBicubic;
theGraphicsCompositingQuality = SystemDrawingDrawingDCompositingQualityHighQuality;
theGraphicsSmoothingMode = SystemDrawingDrawingDSmoothingModeHighQuality;
theGraphicsTextRenderingHint = SystemDrawingTextTextRenderingHintAntiAlias;
// 背景
theGraphicsClear(backgroundColor);

  GraphicsPath gp = new GraphicsPath();
FontFamily fontFamily;
try
{
fontFamily = new FontFamily(fontName);
}
catch (Exception exc)
{
fontFamily = new FontFamily(宋體);
}
StringFormat format = new StringFormat();
formatAlignment = StringAlignmentNear;
formatLineAlignment = StringAlignmentCenter;

  gpAddString(text fontFamily f new Point( ) format);
RectangleF rectF = gpGetBounds();
float scaleX = width * f / rectFWidth;
float scaleY = height * f / rectFHeight;
float offsetX = rectFX * scaleX;
float offsetY = rectFY * scaleY;
SystemDrawingDrawingDMatrix matrix = new Matrix(scaleX scaleY

  offsetX offsetY);
gpTransform(matrix);
Brush newBrush = new SolidBrush(fontColor);
theGraphicsFillPath(newBrush gp);
//theGraphicsDrawRectangle(PensBlack width height);

  if (gp != null) gpDispose();

  // 將生成的圖片發回客戶端
MemoryStream ms = new MemoryStream();
theBitmapSave(ms ImageFormatPng);

  ResponseClearContent(); //需要輸出圖象信息 要修改HTTP頭
ResponseContentType = image/Png;
ResponseBinaryWrite(msToArray());
theGraphicsDispose();
theBitmapDispose();
ResponseEnd();
}

  private Color ConvertColor(string colorString)
{
Array knownColors = SystemEnumGetValues(typeof(KnownColor));
foreach (object colorName in knownColors)
{
if (colorStringToLower() == colorNameToString()ToLower())
{
return ColorFromName(colorString);
}
}

  if (colorStringStartsWith(#))
{
return ColorTranslatorFromHtml(colorString);
}

  try
{
int colorargb = intParse(colorString);
return ColorFromArgb(colorargb);
}
finally
{ }

  return ColorBlack;
}
}
}

  調用方法

  BrawDrawCom在線生成個性化標題
(其中text後面傳入要顯示的文字w或width參數設置圖片寬度h或height設置圖片高度f或font設置字體c或color設置標題的顏色)

  OK大功告成

  如果你是使用靜態網頁你可以使用Javascript方式嵌入

  顯示效果圖

  或許你的服務器上沒有安裝你設置的字體(關於如何取得服務器上已安裝字體列表詳見這篇比如下面代碼

  正常的顯示應該是這樣

  如果沒有此字體顯示出來的效果就是這樣

  原因在於我在下面代碼中設置了沒有該字體時則會執行至catch (Exception exc)塊內

  這裡使用了一般帶中文操作系統的服務器上都會自動安裝的宋體字作為默認字體

  

  

  try
{
fontFamily = new FontFamily(fontName);
}
catch (Exception exc)
{
fontFamily = new FontFamily(宋體);
}


還需要更完善?我想你可以使用Cache以提高服務器的效率或者是如果你需要生成靜態html新聞頁面那麼你可以在生成靜態頁面前自動調用WebService將圖片保存下來然後在靜態頁面中直接調用此新聞標題圖片


From:http://tw.wingwit.com/Article/program/net/201311/12703.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.