⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 GDI+图形程序设计第九章源码,此章节为讲述高级二维图形,包括颜色混合,Alpha混合等高级应用.
💻 CS
📖 第 1 页 / 共 3 页
字号:
	// Use different brush to draw different graphics
	// object
	g.FillRectangle(rgBrush, 20, 20, 100, 100);
	g.FillEllipse(yrBrush, 140, 20, 200, 200);
	g.DrawString("Color Blending", new Font("Tahoma", 30),
		rbBrush, new RectangleF(100, 240, 300, 300) ); 
	// Dispose
	g.Dispose();
}

		private void AlphaBlending_Click(object sender, System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create Image from a file
			Image curImage = Image.FromFile("Neel3.jpg");
			g.DrawImage(curImage, 0, 0, curImage.Width, curImage.Height);
			// Create opaque, semi transparent, and full 
			// transparent pens
			Pen opqPen =
				new Pen(Color.FromArgb(255, 0, 255, 0), 10);
			Pen transPen = 
				new Pen(Color.FromArgb(128, 0, 255, 0), 10);
			Pen totTransPen = 
				new Pen(Color.FromArgb(40, 0, 255, 0), 10);
			// Use different pen to draw different graphics
			// object
			g.DrawLine(opqPen, 10, 10, 200, 10);
			g.DrawLine(transPen, 10, 30, 200, 30);
			g.DrawLine(totTransPen, 10, 50, 200, 50);
			SolidBrush semiTransBrush = 
				new SolidBrush(Color.FromArgb(90, 255, 255, 0));
			g.DrawString("Atlanta Photo \nDate: 04/09/2001",
				new Font("Verdana", 14), semiTransBrush,
				new RectangleF(20, 100, 300, 100) );
			// Dispose
			g.Dispose();
		}

		private void EllipseMenu_Click(object sender, System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Set smmoothing mode and compositing quality
            g.SmoothingMode = SmoothingMode.AntiAlias;
			g.CompositingQuality= 
				CompositingQuality.HighQuality;
			// Create a graphics path and
			// add an ellipse to it
			GraphicsPath path = new GraphicsPath();
			path.AddEllipse(20, 20, 200, 200);
			// Create a path gradient brush with the path
			PathGradientBrush pthGrBrush =
				new PathGradientBrush(path);
			// Create a semi transparent color
			Color transBlackColor = 
				Color.FromArgb(90, 255, 255, 255);
            // Set SurroundColors and CenterColor 
			// of the brush
			Color[] color = {Color.Black};
			pthGrBrush.SurroundColors = color;
			pthGrBrush.CenterColor = Color.Red;
			// Use bush to fill a path
			g.FillPath(pthGrBrush, path);
			// Dispose
			g.Dispose();
		}

		private void RectangleMenu_Click(object sender, System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);

			LinearGradientBrush rgBrush = 
				new LinearGradientBrush(
				new RectangleF(0, 0, /*50*/ 200, /*50 */ 200), 
				Color.Red, Color.Green,
				LinearGradientMode.Horizontal);
			g.FillRectangle(rgBrush, 0, 0, 200, 50);
		
			// Dispose
			g.Dispose();
		}

		private void MixedBlendingMenu_Click(object sender, System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create a Image from a file
			Image curImage = Image.FromFile("Neel3.jpg");
			// Draw Image
			g.DrawImage(curImage, 0, 0, 
				curImage.Width, curImage.Height);
			// Create a graphics path and add a rectangle
			GraphicsPath path = new GraphicsPath();
			path.AddRectangle(new Rectangle(0, 0, 182, 300));
			// Create a path gradient brush
			PathGradientBrush grBrush = 
				new PathGradientBrush(path);
			Color transBlackColor = 
				Color.FromArgb(60, 0, 255, 0);
			// Create a array of colors
			Color[] color = {transBlackColor};
			// Set surround and center colros
			grBrush.SurroundColors = color;
			grBrush.CenterColor = Color.Yellow;
			// Fill path
			g.FillPath(grBrush, path);

			// Dispose
			g.Dispose();
		}

		private void SetBlendTriangularShapeMenu_Click(object sender,
			System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create a rectangle
            Rectangle rect = new Rectangle(20, 20, 100, 50);
            // Create a linear gradient brush
            LinearGradientBrush rgBrush = 
                new LinearGradientBrush(
                rect, Color.Red, Color.Green,
                0.0f, true);        
            // Fill rectangle
            g.FillRectangle(rgBrush, rect);
            rect.Y = 90;
            // set blend triangular shape
            rgBrush.SetBlendTriangularShape(0.5f, 1.0f);            
            // Fill rectangle again
            g.FillRectangle(rgBrush, rect);
			// Dispose
			g.Dispose();		
		}

		private void SetSigmaBellShapeMenu_Click(object sender, 
			System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create a rectangle
			Rectangle rect = new Rectangle(20, 20, 100, 50);
			// Create a linear gradient brush
			LinearGradientBrush rgBrush = 
				new LinearGradientBrush(
				rect, Color.Red, Color.Green,
				0.0f, true);			
			// Fill rectangle
			g.FillRectangle(rgBrush, rect);
			rect.Y = 90;
			// set signma bell shape
			rgBrush.SetSigmaBellShape(0.5f, 1.0f);			
			// Fill rectangle again
			g.FillRectangle(rgBrush, rect);	
			// Dispose
			g.Dispose();		
		}

		private void BlendPropMenu_Click(object sender,
			System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create a linear gradient brush
			LinearGradientBrush brBrush = 
				new LinearGradientBrush(
				new Point(0, 0), new Point(50, 20),
				Color.Blue, Color.Red);  
			// Create a Blend object
			Blend blend = new Blend();
			float[] factArray = {0.0f, 0.3f, 0.5f, 1.0f};
			float[] posArray   = {0.0f, 0.2f, 0.6f, 1.0f};
			// Set Blend's factors and positions
			blend.Factors = factArray;
			blend.Positions = posArray;
			// Set Blend property of the brush
			brBrush.Blend = blend;
            // Fill a rectangel and ellipse
			g.FillRectangle(brBrush, 10, 20, 200, 100);
			g.FillEllipse(brBrush, 10, 150, 120, 120);		
			// Dispose
			g.Dispose();
		}

		private void InterpolationColorsMenu_Click
			(object sender,	System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create a LinearGradientBrush
			LinearGradientBrush brBrush = 
				new LinearGradientBrush(
				new Point(0, 0), new Point(50, 20),
				Color.Blue, Color.Red);  
			Rectangle rect = 
				new Rectangle(20, 20, 200, 100);
			// Create color and points array
			Color[] clrArray = 
			{
				Color.Red, Color.Blue, Color.Green,
                Color.Pink, Color.Yellow, 
				Color.DarkTurquoise
			};  
			float[] posArray = 
			{
				0.0f, 0.2f, 0.4f, 
				0.6f, 0.8f, 1.0f
			};    
			// Create a ColorBlend object and 
			// set its Colors and positions
			ColorBlend colorBlend = new ColorBlend();
			colorBlend.Colors = clrArray;
			colorBlend.Positions = posArray;
			// Set InterpolationColors property
			brBrush.InterpolationColors = colorBlend;
			// Draw shapes		
			g.FillRectangle(brBrush, rect);
			rect.Y = 150;
			rect.Width = 100;
			rect.Height = 100;
			g.FillEllipse(brBrush, rect);			
			// Dispose
			g.Dispose();		
		}

		private void GammaCorrectionMenu_Click(
			object sender, System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create a rectangle
			Rectangle rect = 
				new Rectangle(20, 20, 100, 50);
			// Create a linear gradient brush
			LinearGradientBrush rgBrush = 
				new LinearGradientBrush(
				rect, Color.Red, Color.Green,
				0.0f, true);		
			// Fill rectangle
			g.FillRectangle(rgBrush, rect);
			rect.Y = 90;
			// Set gamma collection of the brush
			rgBrush.GammaCorrection = true; 
			// Fill rectangle
			g.FillRectangle(rgBrush, rect);
			// Dispose
			g.Dispose();	
		}

		private void PathGradientProsMenu_Click(object sender, System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Set smoothing mode as anti alias
			g.SmoothingMode = SmoothingMode.AntiAlias;
			// Create a graphics path and add a rectangle
			GraphicsPath path = new GraphicsPath();
			Rectangle rect = 
				new Rectangle(10, 20, 200, 200);
			path.AddRectangle(rect);
			// Create path gradient brush 
			PathGradientBrush rgBrush = 
				new PathGradientBrush(path);
			// Set brush's center and focusscale colors
			rgBrush.CenterColor = Color.Red; 
			rgBrush.FocusScales = 
				new PointF(0.6f, 0.2f);
			Color[] colors = {Color.Green, 
			Color.Blue, Color.Red, Color.Yellow};
			// Set surrounded colors
			rgBrush.SurroundColors = colors;
			// Fill ellipse
			g.FillEllipse(rgBrush, rect);
			// Dispose
			g.Dispose();

		}

		private void PathGBInterPol_Click(object sender,
			System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create color and points array
			Color[] clrArray = 
				{Color.Red, Color.Blue, Color.Green,
  			   Color.Pink, Color.Yellow, 
					Color.DarkTurquoise};  
			float[] posArray = 
				{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f};    
			// Create a ColorBlend object and set its Colors and 
			// positions
			ColorBlend colorBlend = new ColorBlend();
			colorBlend.Colors = clrArray;
			colorBlend.Positions = posArray;
			// Set smoothing mode of Graphics
			g.SmoothingMode = SmoothingMode.AntiAlias;
			// Create a Graphics path and add a rectangle
			GraphicsPath path = new GraphicsPath();
			Rectangle rect = new Rectangle(10, 20, 200, 200);
			path.AddRectangle(rect);
			// Create a path gradiant brush
			PathGradientBrush rgBrush = 
					new PathGradientBrush(path);
			// Set interpolation colors and focus scales
			rgBrush.InterpolationColors =  colorBlend; 
			rgBrush.FocusScales = new PointF(0.6f, 0.2f);
			Color[] colors = {Color.Green};
			// Set ceneter and surrounded colors
			rgBrush.CenterColor = Color.Red;
			rgBrush.SurroundColors = colors;
			// Draw ellipse
			g.FillEllipse(rgBrush, rect);
			// Dispose
			g.Dispose();		
		}

		private void PathGBBlend_Click(object sender,
			System.EventArgs e)
		{			
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Creat Blend object
			Blend blend = new Blend();
			// Create point and position arrays
			float[] factArray = {0.0f, 0.3f, 0.5f, 1.0f};
			float[] posArray   = {0.0f, 0.2f, 0.6f, 1.0f};
			// Set factors and positions of Blend
			blend.Factors = factArray;
			blend.Positions = posArray;
			// Set smoothing mode of Graphics

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -