關於GDI+ 的使用,就對點,線,面的畫的操做,圖像剪裁,縮放等等操做,瞭解各類經常使用的方法和屬性。算法
經常使用命名空間:System.Drawing;System.Drawing.Image;System.Drawing.Drawing2D;ide
Graphics類封裝了一個GDI+繪圖圖面,提供將對象繪製到顯示到設備的方法。Graphics叫畫板,只不過這個畫板中帶了不少工具。但畫圖時你要定義畫板的大小,顏色等等,還應給他一張畫紙;工具
Graphicsthis
1.建立Graphics基本方法:spa
Graphics g = this.CreateGraphics();orm
Graphics g = e;// Paint事件中的對象
Graphics g = Graphics.FromImage();//Graphics.Fromxx類的各類靜態方法。事件
誰建立Graphics對象,就在誰上畫。圖片
2.畫的方法:ip
g.Drawxx 的各類方法。
3.Graphics用的 畫筆和畫刷
pen 和 Font
pen.PenType //屬性
pen.DashStyle
Font f = new Font( "宋體", 15, FontStyle.Bold | FontStyle.Italic );
Brush //畫刷
派生類:
LinearGradientBrush//漸變畫刷
SolidBrush//單色畫刷
HatchBrush //用陰影樣式 (機械製圖時用的多)
TextureBrush//畫字
ImageBrush//圖片畫刷
VisualBrush//
RadialGradientBrush
DrawingBrush
4.圖片處理
1. Graphics.SmoothingMode //消除鋸齒經常使用
2. Graphics.InterpolationMode //圖像縮放經常使用
3. Graphics.CompositingQuality //
5. clear()方法:
Graphics g.clear(Color.Blue);// 不是清除xx顏色,是清除背景並設置xx顏色。
Bitmap ,image 和 Icon
Bitmap bmp = new Bitmap(16, 16);
Bitmap bmp1 = Bitmap.FromHbitmap(bmp.GetHbitmap());
Image image = Image.FromFile(@"C:\\temp.jpg");
bmp.MakeTransparent(Color.FromArgb(255, 0, 255));//把xx顏色設置透明色
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
if (bmp.GetPixel(i, j) == Color.Blue)//獲取像素設置
{
bmp.SetPixel(i, j, Color.Red);
}
}
}
ImageAttributes imageAttr = new ImageAttributes();//經過位圖和圖元文件顏色的信息設置顏色
imageAttr.SetColorKey(lowerColor,upperColor, ColorAdjustType.Default);
e.Graphics.DrawImage(Image, rect, 0, 0, 100, 100, GraphicsUnit.Pixel, imageAttr);
Stream IconStream = System.IO.File.OpenWrite(fileName);
Bitmap bitmap = new Bitmap(pbImage.Image);
bitmap.SetResolution(32, 32);
Icon icon = System.Drawing.Icon.FromHandle(bitmap.GetHicon());
icon.Save(IconStream);//比bitmap.save格式強點
其餘經常使用:
Clipboard.SetDataObject(this.pbSource.Image);//截圖
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Bitmap))
image = (Bitmap)data.GetData(DataFormats.Bitmap);
Color c = KnownColor.Control;
Color c =SystemColors.Control
Color c = Color.FromArgb(128, Color.Blue); //128爲半透明顏色
this.Opacity = 0.5//窗體的透明度
System.Drawing.Drawing2D 命名空間下GraphicsPath
//建立矢量圖
Bitmap bmp = new Bitmap(220,220);
Graphics g = Graphics.FromImage(bmp);
Metafile mf = new Metafile(filePath,g.GetHdc());
//畫圖...
g.Save();
g.Dispose();
mf.Dispose();
防止圖片閃爍,雙緩衝設置
SetStyle(ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor, true);
儘可能不要用窗體TransparencyKey不然閃爍和卡頓會使用閃爍更嚴重。
不要在Paint事件給各類 xx.Image 賦值,xx.Image會調用paint這樣會死循環。
圖像的各類效果(底片、浮雕、黑白、濾鏡)只是算法問題。