本文整理汇总了C#中System.Drawing.Drawing2D.GraphicsPath.Warp方法的典型用法代码示例。如果您正苦于以下问题:C# System.Drawing.Drawing2D.GraphicsPath.Warp方法的具体用法?C# System.Drawing.Drawing2D.GraphicsPath.Warp怎么用?C# System.Drawing.Drawing2D.GraphicsPath.Warp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Drawing2D.GraphicsPath
的用法示例。
在下文中一共展示了System.Drawing.Drawing2D.GraphicsPath.Warp方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
}
示例2: 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;
}