c# winform 应用编程代码总结 7
24、-在一个区域中剪辑
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Create a path that consists of a single polygon.
Point[] polyPoints = {
new Point(10, 10),
new Point(150, 10),
new Point(100, 75),
new Point(100, 150)};
GraphicsPath path = new GraphicsPath();
path.AddPolygon(polyPoints);
// Construct a region based on the path.
Region region = new Region(path);
// Draw the outline of the region.
Pen pen = Pens.Black;
e.Graphics.DrawPath(pen, path);
// Set the clipping region of the Graphics object.
e.Graphics.SetClip(region, CombineMode.Replace);
// Draw some clipped strings.
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
fontFamily,
36, FontStyle.Bold,
GraphicsUnit.Pixel);
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 255, 0, 0));
//e.Graphics.Clip=region;
e.Graphics.DrawString(
"A Clipping Region",
font, solidBrush,
new PointF(15, 25));
e.Graphics.DrawString(
"A Clipping Region",
font,
solidBrush,
new PointF(15, 68));
}
效果:
25、使用颜色再变换表(改变图像中某种图片的颜色)
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Image image = new Bitmap("..\\..\\test.bmp");
int width = image.Width;
int height = image.Height;
e.Graphics.DrawImage(image, 10, 10, width, height);
ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 255, 0, 0); // opaque red
colorMap.NewColor = Color.FromArgb(255, 0, 0, 255); // opaque blue
ColorMap[] remapTable = {colorMap};
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
e.Graphics.DrawImage(
image,
new Rectangle(180, 10, width, height), // destination rectangle
0, 0, // upper-left corner of source rectangle
width, // width of source rectangle
height, // height of source rectangle
GraphicsUnit.Pixel,
imageAttributes);
}
效果:
26、消除文本的走样现象
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
FontFamily fontFamily = new FontFamily("Times New Roman");
Font font = new Font(
fontFamily,
32,
FontStyle.Regular,
GraphicsUnit.Pixel);
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
string string1 = "SingleBitPerPixel";
string string2 = "AntiAlias";
e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
e.Graphics.DrawString(string1, font, solidBrush, new PointF(10, 10));
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
e.Graphics.DrawString(string2, font, solidBrush, new PointF(10, 60));
}
效果图:
27、绘制垂直文本
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{string text = "Vertical text";
PointF pointF = new PointF(40, 10);
FontFamily fontFamily = new FontFamily("Lucida Console");
Font font = new Font(
fontFamily,
14,
FontStyle.Regular,
GraphicsUnit.Point);
StringFormat stringFormat = new StringFormat();
stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
e.Graphics.DrawString(text, font, solidBrush, pointF, stringFormat);
}
本系列文章是作者学习《Visual C#.NET 应用编程150例》(源码)心得笔记,欢迎转载,请注明原文地址,如有疑问,可以通过 278250658@qq.com 联系作者本人。
作者:BuildNewApp
出处:http://syxchina.cnblogs.com、 BuildNewApp.com
本文版权归作者、博客园和百度空间共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则作者会诅咒你的。
如果您阅读了我的文章并觉得有价值请点击此处,谢谢您的肯定1。