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

📄 form1.cs

📁 GDI+图形程序设计第九章源码,此章节为讲述高级二维图形,包括颜色混合,Alpha混合等高级应用.
💻 CS
📖 第 1 页 / 共 3 页
字号:
			g.SmoothingMode = SmoothingMode.AntiAlias;
			// Create 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 Blend and FocusScales properties
			rgBrush.Blend = blend; 
			rgBrush.FocusScales = new PointF(0.6f, 0.2f);
			Color[] colors = 
			{
				Color.Green, Color.Blue, 
				Color.Red, Color.Yellow
			};

			// Set CenterColor and SurroundColors 
			rgBrush.CenterColor = Color.Red;
			rgBrush.SurroundColors = colors;
			g.FillEllipse(rgBrush, rect);
			// Dispose
			g.Dispose();	
		}

		private void AlphaBPensBrushes_Click(object sender,
			System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create pens with semi transparent colors
			Rectangle rect = 
				new Rectangle(220, 30, 100, 50); 
			Pen transPen = 
				new Pen(Color.FromArgb(128, 255, 255, 255), 10);
			Pen totTransPen = 
				new Pen(Color.FromArgb(40, 0, 255, 0), 10);
			// Draw line, rectangle, ellipse, string using
			// semi transparent colored pens
			g.DrawLine(transPen, 10, 30, 200, 30);
			g.DrawLine(totTransPen, 10, 50, 200, 50);
			g.FillRectangle(new SolidBrush(
				Color.FromArgb(40, 0, 0, 255)), rect);
			rect.Y += 60;
			g.FillEllipse(new SolidBrush(
				Color.FromArgb(20, 255, 255, 0)), rect);
			SolidBrush semiTransBrush = 
				new SolidBrush(Color.FromArgb(90, 0, 50, 255));
			g.DrawString("Some Photo \nDate: 04/09/2001", 
				new Font("Verdana", 14), semiTransBrush,
				new RectangleF(20, 100, 300, 100) );
			// Dispose
			g.Dispose();
		}

		private void AlphaBImages_Click(object sender,
			System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Draw an image 
			Image curImage = 
				Image.FromFile("Neel3.jpg");
			g.DrawImage(curImage, 0, 0, 
				curImage.Width, curImage.Height);
			// Create pens and a rectangle
			Rectangle rect = 
				new Rectangle(220, 30, 100, 50); 
			Pen opqPen = 
				new Pen(Color.FromArgb(255, 0, 255, 0), 10);
			Pen transPen = 
				new Pen(Color.FromArgb(128, 255, 255, 255), 10);
			Pen totTransPen = 
				new Pen(Color.FromArgb(40, 0, 255, 0), 10);	
			// Draw lines, rectangle, ellipse and string
			g.DrawLine(opqPen, 10, 10, 200, 10);
			g.DrawLine(transPen, 10, 30, 200, 30);
			g.DrawLine(totTransPen, 10, 50, 200, 50);
			g.FillRectangle(new SolidBrush(
				Color.FromArgb(140, 0, 0, 255)), rect);
			rect.Y += 60;
			g.FillEllipse(new SolidBrush(
				Color.FromArgb(150, 255, 255, 255)), rect);
			SolidBrush semiTransBrush = 
				new SolidBrush(Color.FromArgb(90, 255, 255, 50));
			g.DrawString("Some Photo \nDate: 04/09/2001", 
				new Font("Verdana", 14), semiTransBrush,
				new RectangleF(20, 100, 300, 100) );
			// Dispose
			g.Dispose();
		}

		private void AlphaBCompGammaCorr_Click(object sender,
			System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create two rectangles
			Rectangle rect1 = 
				new Rectangle(20, 20, 100, 100);
			Rectangle rect2 = 
				new Rectangle(200, 20, 100, 100);
			// Create two SolidBrush objects
			SolidBrush redBrush = 
				new SolidBrush(Color.FromArgb(150, 255, 0, 0));
			SolidBrush greenBrush = 
				new SolidBrush(Color.FromArgb(180, 0, 255, 0));
			// Create a Bitmap
			Bitmap tempBmp = new Bitmap(200, 150);
			// Create a Graphics objects
			Graphics tempGraphics = 
				Graphics.FromImage(tempBmp);		
			// Set Compositing mode and compositing
			// quality of Graphics object
			tempGraphics.CompositingMode = 
				CompositingMode.SourceOver;
			tempGraphics.CompositingQuality = 
				CompositingQuality.GammaCorrected;
			//tempGraphics.CompositingMode = 
				//CompositingMode.SourceCopy;
			// Fill recntagle
			tempGraphics.FillRectangle(redBrush, rect1);
			rect1.X += 30;
			rect1.Y += 30;
			// Fill ellipse
			tempGraphics.FillEllipse(greenBrush, rect1);		
			g.CompositingQuality = 
				CompositingQuality.GammaCorrected;		
			// Draw image
			g.DrawImage(tempBmp, 0, 0);		
            // Fill rectangle			
			g.FillRectangle(Brushes.Red, rect2);
			rect2.X += 30;
			rect2.Y += 30;
			// Fill ellipse
			g.FillEllipse(Brushes.Green, rect2);
			// Dispose
			greenBrush.Dispose();
			redBrush.Dispose();
			tempBmp.Dispose();
			g.Dispose();		
		}

		private void AlphaBMatrix_Click(object sender, System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create a rectangle 
			Rectangle rect = new Rectangle(20, 20, 200, 100);
			// Create a Bitmap object from a file
			Bitmap bitmap = new Bitmap("MyPhoto.jpg");
			// Create a points array
			float[][] ptsArray =
			{ 
			   new float[] {1, 0, 0, 0, 0},
			   new float[] {0, 1, 0, 0, 0},
			   new float[] {0, 0, 1, 0, 0},
			   new float[] {0, 0, 0, 0.5f, 0}, 
			   new float[] {0, 0, 0, 0, 1}
			}; 
			// Create a ColorMatrix using pts array
			ColorMatrix clrMatrix = 
				new ColorMatrix(ptsArray);
			// Create an ImageAttributes object
			ImageAttributes imgAttributes = 
				new ImageAttributes();
			// Set ColorMatrix of ImageAttributes
			imgAttributes.SetColorMatrix(clrMatrix,
				ColorMatrixFlag.Default,
				ColorAdjustType.Bitmap);
			// Fill rectangle
			g.FillRectangle(Brushes.Red, rect);
			rect.Y += 120;
			// Fill ellipse
			g.FillEllipse(Brushes.Black, rect);
			// Draw image using ImageAttributes
			g.DrawImage(bitmap, 
				new Rectangle(0, 0, 
				bitmap.Width, bitmap.Height),  
				0, 0, bitmap.Width, bitmap.Height,
				GraphicsUnit.Pixel, imgAttributes);
			// Dispose
			g.Dispose();
		}

		private void CompBlendTSigmaBell_Click(object sender, 
			System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create a rectangle
			Rectangle rect = new Rectangle(0, 0, 40, 20);
			// Create a linear gradient brush
			LinearGradientBrush rgBrush = 
				new LinearGradientBrush(
				rect, Color.Black, Color.Blue,
				0.0f, true);            
			// Fill rectangle
			g.FillRectangle(rgBrush, 
				new Rectangle(10, 10, 300, 100));
			// set signma bell shape
			rgBrush.SetSigmaBellShape(0.8f, 1.0f);
			// Fill rectangle again
			g.FillRectangle(rgBrush, 
				new Rectangle(10, 120, 300, 100)); 
			// set blend triangular shape
			rgBrush.SetBlendTriangularShape(0.2f, 1.0f);
			// Fill rectangle again
			g.FillRectangle(rgBrush, 
				new Rectangle(10, 240, 300, 100)); 
			// Dispose
			g.Dispose();
		}

		private void MixedBlending_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;

			// Create a Bitmap object from a file
			Bitmap bitmap = new Bitmap("Neel3.jpg");
			// Create a points array
			float[][] ptsArray =
			{ 
				new float[] {1, 0, 0, 0, 0},
				new float[] {0, 1, 0, 0, 0},
				new float[] {0, 0, 1, 0, 0},
				new float[] {0, 0, 0, 0.5f, 0}, 
				new float[] {0, 0, 0, 0, 1}
			}; 
			// Create a ColorMatrix using pts array
			ColorMatrix clrMatrix = 
				new ColorMatrix(ptsArray);
			// Create an ImageAttributes object
			ImageAttributes imgAttributes = 
				new ImageAttributes();
			// Set ColorMatrix of ImageAttributes
			imgAttributes.SetColorMatrix(clrMatrix,
				ColorMatrixFlag.Default,
				ColorAdjustType.Bitmap);
			// Fill rectangle
			g.FillRectangle(brBrush, rect);
			rect.Y += 120;
			// Fill ellipse
			g.FillEllipse(brBrush, rect);
			// Draw image using ImageAttributes
			g.DrawImage(bitmap, 
				new Rectangle(0, 0, 
				bitmap.Width, bitmap.Height),  
				0, 0, bitmap.Width, bitmap.Height,
				GraphicsUnit.Pixel, imgAttributes);

			// Dispose
			brBrush.Dispose();
			bitmap.Dispose();
			g.Dispose();
		}

		private void BlendCenter_Click(object sender, System.EventArgs e)
		{
			
		}

		private void CompBlendTSigmaBellMEnu_Click(object sender, 
			System.EventArgs e)
		{
			Graphics g = this.CreateGraphics();
			g.Clear(this.BackColor);
			// Create a rectangle
			Rectangle rect = new Rectangle(0, 0, 40, 20);
			// Create a linear gradient brush
			LinearGradientBrush rgBrush = 
				new LinearGradientBrush(
				rect, Color.Black, Color.Blue,
				0.0f, true);            
			// Fill rectangle
			g.FillRectangle(rgBrush, 
				new Rectangle(10, 10, 300, 100));
			// set signma bell shape
			rgBrush.SetSigmaBellShape(0.5f, 1.0f);
			// Fill rectangle again
			g.FillRectangle(rgBrush, 
				new Rectangle(10, 120, 300, 100)); 
			// set blend triangular shape
			rgBrush.SetBlendTriangularShape(0.5f, 1.0f);
			// Fill rectangle again
			g.FillRectangle(rgBrush, 
				new Rectangle(10, 240, 300, 100)); 
			// Dispose
			g.Dispose();
		}
	}
}










⌨️ 快捷键说明

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