c#

位置:IT落伍者 >> c# >> 浏览文章

C#中使用GDI 让网站新闻标题个性化


发布日期:2022年12月02日
 
C#中使用GDI 让网站新闻标题个性化

新华网上的今日头条的标题是用图片体现的这种头条我们一般的做法可能是使用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将图片保存下来然后在静态页面中直接调用此新闻标题图片

               

上一篇:应用开发技巧 C#中加强ListView控件的功能

下一篇:ADO.NET性能改善方法集合