本文整理汇总了C#中System.Drawing.Drawing2D.GraphicsPath.AddString方法的典型用法代码示例。如果您正苦于以下问题:C# System.Drawing.Drawing2D.GraphicsPath.AddString方法的具体用法?C# System.Drawing.Drawing2D.GraphicsPath.AddString怎么用?C# System.Drawing.Drawing2D.GraphicsPath.AddString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Drawing2D.GraphicsPath
的用法示例。
在下文中一共展示了System.Drawing.Drawing2D.GraphicsPath.AddString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DrawLabel
/// <summary>
/// Renders a label to the map.
/// </summary>
/// <param name="g">Graphics reference</param>
/// <param name="LabelPoint">Label placement</param>
/// <param name="Offset">Offset of label in screen coordinates</param>
/// <param name="font">Font used for rendering</param>
/// <param name="forecolor">Font forecolor</param>
/// <param name="backcolor">Background color</param>
/// <param name="halo">Color of halo</param>
/// <param name="rotation">Text rotation in degrees</param>
/// <param name="text">Text to render</param>
/// <param name="map">Map reference</param>
public static void DrawLabel(System.Drawing.Graphics g, System.Drawing.PointF LabelPoint, System.Drawing.PointF Offset, System.Drawing.Font font, System.Drawing.Color forecolor, System.Drawing.Brush backcolor, System.Drawing.Pen halo, float rotation, string text, SharpMap.Map map)
{
System.Drawing.SizeF fontSize = g.MeasureString(text, font); //Calculate the size of the text
LabelPoint.X += Offset.X; LabelPoint.Y += Offset.Y; //add label offset
if (rotation != 0 && rotation != float.NaN)
{
g.TranslateTransform(LabelPoint.X, LabelPoint.Y);
g.RotateTransform(rotation);
g.TranslateTransform(-fontSize.Width / 2, -fontSize.Height / 2);
if (backcolor != null && backcolor != System.Drawing.Brushes.Transparent)
g.FillRectangle(backcolor, 0, 0, fontSize.Width * 0.74f + 1f, fontSize.Height * 0.74f);
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, font.Size, new System.Drawing.Point(0, 0), null);
if (halo != null)
g.DrawPath(halo, path);
g.FillPath(new System.Drawing.SolidBrush(forecolor), path);
//g.DrawString(text, font, new System.Drawing.SolidBrush(forecolor), 0, 0);
g.Transform = map.MapTransform;
}
else
{
if (backcolor != null && backcolor != System.Drawing.Brushes.Transparent)
g.FillRectangle(backcolor, LabelPoint.X, LabelPoint.Y, fontSize.Width * 0.74f + 1, fontSize.Height * 0.74f);
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, font.Size, LabelPoint, null);
if (halo != null)
g.DrawPath(halo, path);
g.FillPath(new System.Drawing.SolidBrush(forecolor), path);
//g.DrawString(text, font, new System.Drawing.SolidBrush(forecolor), LabelPoint.X, LabelPoint.Y);
}
}
示例2: draw
private void draw(object sender, EventArgs e)
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.FillRectangle(new SolidBrush(color), new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
graphicsPath.AddString(textBox2.Text, font.FontFamily, (int)FontStyle.Regular, (int)numericUpDown4.Value, new Point((int)numericUpDown1.Value, (int)numericUpDown2.Value), StringFormat.GenericDefault);
graphics.FillPath(Brushes.White, graphicsPath);
graphics.DrawPath(new Pen(Color.Black, (int)numericUpDown3.Value), graphicsPath);
}
pictureBox1.Image = bitmap;
}
示例3: OnPaint
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
_theGame.Draw(e.Graphics, _interp);
// draw fps
var path = new System.Drawing.Drawing2D.GraphicsPath();
Pen p = new Pen(Color.Black, 2.0f);
path.AddString(String.Format("FPS: {0}", _fpsCalc.Fps),
FontFamily.GenericSansSerif, 0, 24f,
Point.Empty, StringFormat.GenericTypographic);
e.Graphics.DrawPath(p, path);
e.Graphics.FillPath(Brushes.White, path);
p.Dispose();
path.Dispose();
_fpsCalc.DrawFrame(); // used for calculating FPS
}
示例4: MeasureText
private Size MeasureText(Graphics graphics, string text, Font font)
{
var sz = TextRenderer.MeasureText(graphics, text, this.Font, new Size(0, 0), tf);
return sz;
var bounds = new RectangleF();
using (var textPath = new System.Drawing.Drawing2D.GraphicsPath())
{
textPath.AddString(
text,
font.FontFamily,
(int)font.Style,
font.Size,
new PointF(0, 0),
StringFormat.GenericTypographic);
bounds = textPath.GetBounds();
}
return new Size((int)bounds.Width, (int)bounds.Height);
}
示例5: BurnNewWallpaper
/// <summary>
/// Bakes the current time on the original wallpaper and returns the path of the baked wallpaper.
/// </summary>
/// <param name="originalWallpaperPath">The full path of the original wallpaper.</param>
/// <returns></returns>
public static string BurnNewWallpaper(string originalWallpaperPath)
{
Image wallpaper = Image.FromFile(originalWallpaperPath);
Bitmap bufferBtmp = new Bitmap(wallpaper);
Graphics wallpaperGraph = Graphics.FromImage(bufferBtmp);
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
wallpaperGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
wallpaperGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
path.FillMode = System.Drawing.Drawing2D.FillMode.Winding;
path.AddString(DateTime.Now.ToShortTimeString(), font.FontFamily, (int)font.Style, (float)(fontSize * Convert.ToDouble(wallpaper.Width) / Convert.ToDouble(pictureBoxRectangle.Width)), CalculateTheRelativePoint(), new StringFormat());
wallpaperGraph.DrawPath(new Pen(HSL.GetComplementaryColor(color), 3f), path);
wallpaperGraph.FillPath(new SolidBrush(Color.FromArgb(255, color)), path);
BurntWallpaperPath = Settings.rootDirectory + "\\wallpaper.png";
bufferBtmp.Save(BurntWallpaperPath);
bufferBtmp.Dispose();
wallpaper.Dispose();
wallpaperGraph.Dispose();
return BurntWallpaperPath;
}
示例6: pictureBox1_MouseMove
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
labelState.Text = e.Location.X.ToString() + " , " + e.Location.Y.ToString();
if (isDrawing)
{
pictureBox1.Refresh();
Color newColor = Color.FromArgb(125, Color.Gray);
Control control = (Control)sender;
formGraphics = control.CreateGraphics();
formGraphics.FillRectangle(new SolidBrush(newColor), theRectangle);
formGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
theRectangle.X = startpoint.X;
theRectangle.Y = startpoint.Y;
theRectangle.Height = e.Y - startpoint.Y;
theRectangle.Width = e.X - startpoint.X;
fontSize = 96f;
SizeF stringSize = formGraphics.MeasureString(DateTime.Now.ToShortTimeString(), new Font(font.Name, fontSize));
if (theRectangle.Width > 15 && theRectangle.Height > 15)
{
while (stringSize.Width > theRectangle.Width || stringSize.Height > theRectangle.Height)
{
stringSize = formGraphics.MeasureString(DateTime.Now.ToShortTimeString(), new Font(font.Name, fontSize));
fontSize -= 0.05f;
}
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddString(DateTime.Now.ToShortTimeString(), font.FontFamily, (int)font.Style, fontSize, theRectangle, new StringFormat());
formGraphics.DrawPath(new Pen(HSL.GetComplementaryColor(color), 3f), path);
formGraphics.FillPath(new SolidBrush(Color.FromArgb(255, color)), path);
Settings.ChangeSetting(Settings.settings.fontSize, fontSize.ToString());
Wallpaper.fontSize = this.fontSize;
}
}
}
示例7: DrawLabel
/// <summary>
/// Draws labels in a specified rectangle
/// </summary>
/// <param name="g">The graphics object to draw to</param>
/// <param name="labelText">The label text to draw</param>
/// <param name="labelBounds">The rectangle of the label</param>
/// <param name="symb">the Label Symbolizer to use when drawing the label</param>
private static void DrawLabel(Graphics g, string labelText, RectangleF labelBounds, ILabelSymbolizer symb)
{
//Sets up the brushes and such for the labeling
Brush foreBrush = new SolidBrush(symb.FontColor);
Font textFont = symb.GetFont();
StringFormat format = new StringFormat();
format.Alignment = symb.Alignment;
Pen borderPen = new Pen(symb.BorderColor);
Brush backBrush = new SolidBrush(symb.BackColor);
Brush haloBrush = new SolidBrush(symb.HaloColor);
Pen haloPen = new Pen(symb.HaloColor);
haloPen.Width = 2;
haloPen.Alignment = System.Drawing.Drawing2D.PenAlignment.Outset;
Brush shadowBrush = new SolidBrush(symb.DropShadowColor);
//Text graphics path
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddString(labelText, textFont.FontFamily, (int)textFont.Style, textFont.SizeInPoints * 96F / 72F, labelBounds, format);
//Draws the text outline
if (symb.BackColorEnabled && symb.BackColor != Color.Transparent)
{
if (symb.FontColor == Color.Transparent)
{
System.Drawing.Drawing2D.GraphicsPath backgroundGP = new System.Drawing.Drawing2D.GraphicsPath();
backgroundGP.AddRectangle(labelBounds);
backgroundGP.FillMode = System.Drawing.Drawing2D.FillMode.Alternate;
backgroundGP.AddPath(gp, true);
g.FillPath(backBrush, backgroundGP);
backgroundGP.Dispose();
}
else
{
g.FillRectangle(backBrush, labelBounds);
}
}
//Draws the border if its enabled
if (symb.BorderVisible && symb.BorderColor != Color.Transparent)
g.DrawRectangle(borderPen, labelBounds.X, labelBounds.Y, labelBounds.Width, labelBounds.Height);
//Draws the drop shadow
if (symb.DropShadowEnabled && symb.DropShadowColor != Color.Transparent)
{
System.Drawing.Drawing2D.Matrix gpTrans = new System.Drawing.Drawing2D.Matrix();
gpTrans.Translate(symb.DropShadowPixelOffset.X, symb.DropShadowPixelOffset.Y);
gp.Transform(gpTrans);
g.FillPath(shadowBrush, gp);
gpTrans = new System.Drawing.Drawing2D.Matrix();
gpTrans.Translate(-symb.DropShadowPixelOffset.X, -symb.DropShadowPixelOffset.Y);
gp.Transform(gpTrans);
}
//Draws the text halo
if (symb.HaloEnabled && symb.HaloColor != Color.Transparent)
g.DrawPath(haloPen, gp);
//Draws the text if its not transparent
if (symb.FontColor != Color.Transparent)
g.FillPath(foreBrush, gp);
//Cleans up the rest of the drawing objects
shadowBrush.Dispose();
borderPen.Dispose();
foreBrush.Dispose();
backBrush.Dispose();
haloBrush.Dispose();
haloPen.Dispose();
}
示例8: DrawCharactorsOutLines
private void DrawCharactorsOutLines(ref Graphics g)
{
System.Drawing.Drawing2D.GraphicsPath oOutline = new System.Drawing.Drawing2D.GraphicsPath();
int iSymbolIndex = 0;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
string sCharactor = new string(Convert.ToChar(iSymbolIndex++), 1);
oOutline = new System.Drawing.Drawing2D.GraphicsPath();
oOutline.AddString(sCharactor, this._curFont.FontFamily, (int)FontStyle.Regular, this._fontSize - 7, new Point(j * this._fontSize, i * this._fontSize), StringFormat.GenericDefault);
g.FillPath(new SolidBrush(Color.Black), oOutline);
oOutline.Dispose();
//g.DrawString(sCharactor, this._curFont, new SolidBrush(Color.Black), new RectangleF(j * this._fontSize , i * this._fontSize, this._fontSize, this._fontSize), StringFormat.GenericTypographic);
}
}
}
示例9: Load
// load one single glyph into the cache
private GlyphData Load(char glyph, FormatOptions formatOptions)
{
int fontId = GetFontId(IsAnsiChar(glyph) ? formatOptions.AnsiFont : formatOptions.Font);
var font = m_registeredFonts[fontId];
uint glyphId = ((uint)fontId << 16) + glyph;
GlyphData glyphData;
if (m_loadedGlyphs.TryGetValue(glyphId, out glyphData))
{
glyphData.m_timeStamp = m_timeStamp;
return glyphData;
}
var str = glyph.ToString();
var chRect = MeasureCharacter(glyph, m_registeredFonts[fontId].m_fontObject);
chRect.Inflate(font.m_outlineThickness * 0.5f, font.m_outlineThickness * 0.5f);
int width = Math.Max((int)Math.Ceiling(chRect.Width), 1);
int height = Math.Max((int)Math.Ceiling(chRect.Height), 1);
int pagesInX = (width - 1) / PageSize + 1;
int pagesInY = (height - 1) / PageSize + 1;
GlyphPage[] pages = new GlyphPage[pagesInX * pagesInY];
for (int i = 0; i < pages.Length; ++i)
{
pages[i].m_x = i % pagesInX;
pages[i].m_y = i / pagesInX;
pages[i].m_pageIndex = RequestPage();
}
using (var bmp = new SystemDrawing.Bitmap(pagesInX * PageSize, pagesInY * PageSize))
using (var g = SystemDrawing.Graphics.FromImage(bmp))
using (var memStream = new MemoryStream())
{
// draw text using GDI+
g.TextRenderingHint = SystemDrawing.Text.TextRenderingHint.AntiAliasGridFit;
g.SmoothingMode = SystemDrawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = SystemDrawing.Drawing2D.InterpolationMode.HighQualityBicubic;
if (font.m_outlineThickness > 0)
{
SystemDrawing.Pen outlinePen;
if (!m_outlinePensWithWidths.TryGetValue(font.m_outlineThickness, out outlinePen))
{
outlinePen = new SystemDrawing.Pen(SystemDrawing.Color.Gray, font.m_outlineThickness);
outlinePen.MiterLimit = font.m_outlineThickness;
m_outlinePensWithWidths.Add(font.m_outlineThickness, outlinePen);
}
// draw outline
using (var outlinePath = new SystemDrawing.Drawing2D.GraphicsPath())
{
outlinePath.AddString(str,
font.m_fontObject.FontFamily,
(int)font.m_fontObject.Style,
g.DpiX * font.m_fontObject.SizeInPoints / 72,
new SystemDrawing.PointF(-chRect.Left, -chRect.Top),
SystemDrawing.StringFormat.GenericDefault);
g.DrawPath(outlinePen, outlinePath);
g.FillPath(m_whiteBrush, outlinePath);
}
}
else
{
g.DrawString(str, font.m_fontObject, m_whiteBrush, new SystemDrawing.PointF(-chRect.Left, -chRect.Top));
}
bmp.Save(memStream, System.Drawing.Imaging.ImageFormat.Png);
using (var tmpTexture = Texture2D.FromStream(GameApp.Instance.GraphicsDevice, memStream))
{
var device = GameApp.Instance.GraphicsDevice;
device.DepthStencilState = DepthStencilState.None;
device.RasterizerState = RasterizerState.CullCounterClockwise;
device.Indices = null;
m_effect.CurrentTechnique = m_techBlit;
m_paramTexture.SetValue(tmpTexture);
foreach (var batch in pages.GroupBy(page => page.m_pageIndex / PagesInOneCacheTexture))
{
var textureId = batch.Key;
device.SetRenderTarget(m_cacheTextures[textureId].m_physicalRTTexture);
device.BlendState = m_channelMasks[textureId % 4];
var pagesInBatch = batch.ToArray();
var vertices = new VertexDataBlit[pagesInBatch.Length * 6];
for (int i = 0; i < pagesInBatch.Length; ++i)
{
var page = pagesInBatch[i];
var dstRectLeft = (page.m_pageIndex % PagesInOneCacheTexture) % PagesInOneRow * PageSize;
var dstRectTop = (page.m_pageIndex % PagesInOneCacheTexture) / PagesInOneRow * PageSize;
float posLeft = (dstRectLeft - 0.5f) / CacheTextureSize * 2 - 1;
float posTop = 1 - (dstRectTop - 0.5f) / CacheTextureSize * 2;
float posWidth = PageSize / (float)CacheTextureSize * 2;
float posHeight = -PageSize / (float)CacheTextureSize * 2;
float uvLeft = page.m_x / (float)pagesInX;
float uvTop = page.m_y / (float)pagesInY;
//.........这里部分代码省略.........
示例10: listBox1_SelectedIndexChanged
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (listBox1.SelectedIndex)
{
case 0:
{
MyPen.SetLineCap(System.Drawing.Drawing2D.LineCap.Round, System.Drawing.
Drawing2D.LineCap.Round, System.Drawing.Drawing2D.DashCap.Round);
break;
}
case 1:
{
MyPen.SetLineCap(System.Drawing.Drawing2D.LineCap.ArrowAnchor, System.Drawing.
Drawing2D.LineCap.ArrowAnchor, System.Drawing.Drawing2D.DashCap.Round);
break;
}
case 2:
{
MyPen.SetLineCap(System.Drawing.Drawing2D.LineCap.Square, System.Drawing.
Drawing2D.LineCap.Square, System.Drawing.Drawing2D.DashCap.Round);
break;
}
case 3:
{
MyPen.SetLineCap(System.Drawing.Drawing2D.LineCap.Triangle, System.Drawing.
Drawing2D.LineCap.Triangle, System.Drawing.Drawing2D.DashCap.Round);
break;
}
case 4:
{
MyPen.SetLineCap(System.Drawing.Drawing2D.LineCap.DiamondAnchor, System.Drawing.
Drawing2D.LineCap.DiamondAnchor, System.Drawing.Drawing2D.DashCap.Round);
break;
}
case 5:
{
System.Drawing.Drawing2D.GraphicsPath hPath = new System.Drawing.Drawing2D.GraphicsPath();
Rectangle rect = new Rectangle(0,0,(int)MyPen.Width,(int)MyPen.Width);
hPath.AddString("O", FontFamily.GenericSansSerif, (int)FontStyle.Italic, (float)0.05, rect, StringFormat.GenericDefault);
System.Drawing.Drawing2D.CustomLineCap HookCap = new System.Drawing.Drawing2D.CustomLineCap(null, hPath);
HookCap.SetStrokeCaps(System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.LineCap.Round);
MyPen.CustomStartCap = HookCap;
MyPen.CustomEndCap = HookCap;
MyPen.SetLineCap(System.Drawing.Drawing2D.LineCap.Custom, System.Drawing.
Drawing2D.LineCap.Custom, System.Drawing.Drawing2D.DashCap.Round);
break;
}
}
}
示例11: Render
public Caption Render(string message, Position position, string fontName, float fontSize, bool isBold, bool isItalic, Color fontColor, Color bgColor, float bgBorder)
{
using (Graphics g = Graphics.FromImage(bitmap))
{
float padding = bgBorder * g.DpiX / 72;
float x = padding;
float y = padding;
int fontStyle = (int)System.Drawing.FontStyle.Regular;
if (isBold) fontStyle |= (int)System.Drawing.FontStyle.Bold;
if (isItalic) fontStyle |= (int)System.Drawing.FontStyle.Italic;
Font font = new Font(fontName, fontSize);
Brush brush = new SolidBrush(fontColor);
Brush bbrush = new SolidBrush(bgColor);
Pen pen = new Pen(bbrush, padding);
pen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
// Get Render Bounds
RectangleF rect;
using (var gp = new System.Drawing.Drawing2D.GraphicsPath())
{
gp.AddString(message, new FontFamily(fontName), fontStyle, fontSize, new RectangleF(new PointF(padding, padding), new SizeF(bitmap.Width - padding, bitmap.Height - padding)), StringFormat.GenericTypographic);
rect = gp.GetBounds();
}
// Render
if (position == K4WDisclaimer.Position.Bottom)
{
y = bitmap.Height - rect.Height - rect.Y - padding;
}
using (var gp = new System.Drawing.Drawing2D.GraphicsPath())
{
gp.AddString(message, new FontFamily(fontName), fontStyle, fontSize, new RectangleF(new PointF(x, y), new SizeF(bitmap.Width - padding, bitmap.Height - padding)), StringFormat.GenericTypographic);
g.FillPath(bbrush, gp);
g.DrawPath(pen, gp);
g.FillPath(brush, gp);
}
brush.Dispose();
bbrush.Dispose();
pen.Dispose();
font.Dispose();
}
return this;
}
示例12: GenerateImage
protected void GenerateImage()
{
Random random = new Random();
// Create a new 32-bit bitmap image.
Bitmap bitmap = new Bitmap(230,37,PixelFormat.Format32bppArgb);
// Create a graphics object for drawing.
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Rectangle rect = new Rectangle(0, 0, 230, 37);
// Fill in the background.
System.Drawing.Drawing2D.HatchBrush hatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent50, Color.LightGray, Color.White);
//SolidBrush hatchBrush = new SolidBrush(Color.Red);
String strText =(String)Session["ImageSID"];
g.FillRectangle(hatchBrush, rect);
// Set up the text font.
SizeF size;
float fontSize = rect.Height -12;
// Adjust the font size until the text fits within the image.
Font font = new Font("Century Schoolbook",fontSize, FontStyle.Bold);
size = g.MeasureString(strText, font);
// Set up the text format.
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
// Create a path using the text and warp it randomly.
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddString(
strText,
font.FontFamily,
(int)font.Style,
font.Size, rect,
format);
float v = 12F;
PointF[] points =
{
new PointF(
random.Next(rect.Width) / v,
random.Next(rect.Height) / v),
new PointF(
rect.Width - random.Next(rect.Width) / v,
random.Next(rect.Height) / v),
new PointF(
random.Next(rect.Width) / v,
rect.Height - random.Next(rect.Height) / v),
new PointF(
rect.Width - random.Next(rect.Width) / v,
rect.Height - random.Next(rect.Height) / v)
};
//points={0,0,0,0};
System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
matrix.Translate(0F, 0F);
path.Warp(points, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0F);
// Draw the text.
hatchBrush = new System.Drawing.Drawing2D.HatchBrush(
System.Drawing.Drawing2D.HatchStyle.Percent50,
Color.LightSlateGray,
Color.DarkGray);
g.FillPath(hatchBrush, path);
// Add some random noise.
int m = Math.Max(rect.Width, rect.Height);
for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
{
int x = random.Next(rect.Width);
int y = random.Next(rect.Height);
int w = random.Next(m / 50);
int h = random.Next(m / 50);
g.FillEllipse(hatchBrush, x, y, w, h);
}
// Clean up.
font.Dispose();
hatchBrush.Dispose();
g.Dispose();
// Set the image.
this.Response.Clear();
this.Response.ContentType = "image/jpeg";
// Write the image to the response stream in JPEG format.
bitmap.Save(this.Response.OutputStream, ImageFormat.Jpeg);
// Dispose of the CAPTCHA image object.
bitmap.Dispose();
}
示例13: UnderLine
public ActionResult UnderLine()
{
string sVirtualPath = @"/Content/File/UnderLine/";
//web请求
HttpServerUtility httpServerUtility = System.Web.HttpContext.Current.Server;
if (!Directory.Exists(httpServerUtility.MapPath(sVirtualPath)))
{
//按虚拟路径创建所有目录和子目录
Directory.CreateDirectory(@httpServerUtility.MapPath(sVirtualPath));
}
string index = "1";
string reIndex = Request["index"];
if (!string.IsNullOrEmpty(reIndex))
{
int n;
index = int.TryParse(reIndex, out n) ? n.ToString() : "1";
}
lock (temp)
{
string dFilePath = Server.MapPath(sVirtualPath + "UnderLine" + index + ".png");
//判断图片是否存在
#region 验证空格图片是否存在
if (System.IO.File.Exists(dFilePath))
{
Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png";
Response.BinaryWrite(System.IO.File.ReadAllBytes(dFilePath));
Response.End();
}
#endregion
#region 创建新空格图片
else
{
string filePath = Server.MapPath(sVirtualPath + "UnderLine.gif");
Bitmap bgImg = (Bitmap)Bitmap.FromFile(filePath);
int width = 36;
int height = 18;
Bitmap theBitmap = new Bitmap(width, height);
Graphics theGraphics = Graphics.FromImage(theBitmap);
theGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
theGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
theGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
theGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// 白色背景
//theGraphics.Clear(Color.White);
theGraphics.DrawImage(bgImg, new Rectangle(0, 0, width, height), new Rectangle(0, 0, bgImg.Width, bgImg.Height), GraphicsUnit.Pixel);
//13pt的字体
float fontSize = height * 1.0f / 1.38f;
Font theFont = new Font("Arial", fontSize);
System.Drawing.Drawing2D.GraphicsPath gp = null;
int indLen = index.Length;
Point thePos = new Point((int)(fontSize - (indLen > 1 ? indLen * 2.5 : indLen * 2)), 2);
gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddString(index, theFont.FontFamily, 0, fontSize, thePos, new StringFormat());
theGraphics.DrawPath(new Pen(Color.White, 2f), gp);
theGraphics.FillPath(new SolidBrush(Color.Red), gp);
theGraphics.ResetTransform();
if (gp != null) gp.Dispose();
// 将生成的图片发回客户端
MemoryStream ms = new MemoryStream();
theBitmap.Save(ms, ImageFormat.Png);
//保存文件
theBitmap.Save(dFilePath);
Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
theGraphics.Dispose();
theBitmap.Dispose();
Response.End();
}
#endregion
}
return View();
}
示例14: RenderPreviewFont
public static void RenderPreviewFont(Graphics g, Rectangle size, ITextSymbol item)
{
Font font;
Color? foreground = null;
Color? background = null;
string text = string.Empty;
BackgroundStyleType bgStyle;
if (item == null || item.FontName == null)
{
font = new Font("Arial", 10.0f, FontStyle.Regular); //NOXLATE
foreground = Color.Black;
background = Color.White;
text = Strings.EmptyText;
bgStyle = BackgroundStyleType.Transparent;
}
else
{
try { font = new Font(item.FontName, 12); }
catch { font = new Font("Arial", 12); } //NOXLATE
try
{
foreground = Utility.ParseHTMLColor(item.ForegroundColor);
background = Utility.ParseHTMLColor(item.BackgroundColor);
}
catch { }
bgStyle = item.BackgroundStyle;
FontStyle fs = FontStyle.Regular;
if (item.Bold == "true") //NOXLATE
fs |= FontStyle.Bold;
if (item.Italic == "true") //NOXLATE
fs |= FontStyle.Italic;
if (item.Underlined == "true") //NOXLATE
fs |= FontStyle.Underline;
font = new Font(font, fs);
text = item.Text;
}
if (bgStyle == BackgroundStyleType.Ghosted)
{
using (System.Drawing.Drawing2D.GraphicsPath pth = new System.Drawing.Drawing2D.GraphicsPath())
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pth.AddString(text, font.FontFamily, (int)font.Style, font.Size * 1.25f, size.Location, StringFormat.GenericDefault);
if (background.HasValue)
{
using (Pen p = new Pen(background.Value, 1.5f))
g.DrawPath(p, pth);
}
if (foreground.HasValue)
{
using (Brush b = new SolidBrush(foreground.Value))
g.FillPath(b, pth);
}
}
}
else
{
if (bgStyle == BackgroundStyleType.Opaque)
{
SizeF bgSize = g.MeasureString(text, font, new SizeF(size.Width, size.Height));
if (background.HasValue)
{
using (Brush b = new SolidBrush(background.Value))
g.FillRectangle(b, new Rectangle(size.Top, size.Left, (int)bgSize.Width, (int)bgSize.Height));
}
}
if (foreground.HasValue)
{
using (Brush b = new SolidBrush(foreground.Value))
g.DrawString(text, font, b, size);
}
}
}
示例15: GenerateCaptchaImage
private byte[] GenerateCaptchaImage(string text, int width, int height)
{
Random random = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber));
System.Drawing.Font font;
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bmp);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, width, height);
System.Drawing.Drawing2D.HatchBrush brush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.SmallConfetti, System.Drawing.Color.LightGray, System.Drawing.Color.White);
graphics.FillRectangle(brush, rect);
float emSize = rect.Height + 1;
do {
emSize--;
font = new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, emSize, System.Drawing.FontStyle.Bold);
} while (graphics.MeasureString(text, font).Width > rect.Width);
System.Drawing.StringFormat format = new System.Drawing.StringFormat {
Alignment = System.Drawing.StringAlignment.Center,
LineAlignment = System.Drawing.StringAlignment.Center
};
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, 75f, rect, format);
float num2 = 4f;
System.Drawing.PointF[] destPoints = new System.Drawing.PointF[] { new System.Drawing.PointF(((float)random.Next(rect.Width)) / num2, ((float)random.Next(rect.Height)) / num2), new System.Drawing.PointF(rect.Width - (((float)random.Next(rect.Width)) / num2), ((float)random.Next(rect.Height)) / num2), new System.Drawing.PointF(((float)random.Next(rect.Width)) / num2, rect.Height - (((float)random.Next(rect.Height)) / num2)), new System.Drawing.PointF(rect.Width - (((float)random.Next(rect.Width)) / num2), rect.Height - (((float)random.Next(rect.Height)) / num2)) };
System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
matrix.Translate(0f, 0f);
path.Warp(destPoints, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0f);
brush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent10, System.Drawing.Color.Black, System.Drawing.Color.SkyBlue);
graphics.FillPath(brush, path);
int num3 = Math.Max(rect.Width, rect.Height);
for (int i = 0; i < ((int)(((float)(rect.Width * rect.Height)) / 30f)); i++) {
int x = random.Next(rect.Width);
int y = random.Next(rect.Height);
int w = random.Next(num3 / 50);
int h = random.Next(num3 / 50);
graphics.FillEllipse(brush, x, y, w, h);
}
font.Dispose();
brush.Dispose();
graphics.Dispose();
System.IO.MemoryStream imageStream = new System.IO.MemoryStream();
bmp.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageContent = new Byte[imageStream.Length];
imageStream.Position = 0;
imageStream.Read(imageContent, 0, (int)imageStream.Length);
return imageContent;
}