private void button2_Click(object sender, EventArgs e)
{
Bitmap image = new Bitmap(200, 200);
Graphics g = Graphics.FromImage(image);
//使繪圖品質最高,即消除鋸齒
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
Pen p = new Pen(Color.Blue, 2);//定義了一個藍色,寬度為的畫筆
g.DrawString("字元串", new Font("宋體", 10), new SolidBrush(Color.Blue), new PointF(10, 10));
g.DrawLine(p, 0, 0, 200, 200);//在畫闆上畫直線
g.DrawRectangle(p, 0, 0, 200, 200);//在畫闆上畫矩形
g.DrawEllipse(p, 0, 0, 200, 200);//在畫闆上畫橢圓
//儲存圖檔
image.Save(@"c:\1.jpg");
#region 顯示
//pictureBox1.Image = (Image)image;
//pictureBox1.Image.Save(@"c:\1.jpg");
#endregion
}
private void button2_Click(object sender, EventArgs e)
{
Bitmap image = new Bitmap(400, 800);
Graphics g = Graphics.FromImage(image);
//使繪圖品質最高,即消除鋸齒
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
Pen p = new Pen(Color.Black, 5);//定義了黑色,寬度為5的畫筆
Pen p1 = new Pen(Color.Black, 2);//定義了黑色,寬度為2的畫筆
SolidBrush sb = new SolidBrush(Color.Black); //定義筆刷
g.DrawString("測試", new Font("宋體", 18, FontStyle.Bold), sb, new PointF(20, 20));
g.DrawString("GSM 數字行動電話機", new Font("宋體", 10), sb, new PointF(120, 30));
g.DrawString("黑色 台", new Font("宋體", 12, FontStyle.Bold), sb, new PointF(300, 25));
g.DrawRectangle(p, 0, 0, 400, 800);//在畫闆上畫外框矩形
g.DrawLine(p1, 20, 50, 380, 50);//在畫闆上畫直線
g.DrawRectangle(p1, 20, 120, 360, 650);//在畫闆上畫内框矩形
//粘貼本地條碼圖檔到畫闆
Image image1 = Image.FromFile(@"c:\barcode.gif");
g.DrawImage(image1, 20, 60, image1.Width / 2, image1.Height / 2);
int heightC = 90; //起始Y坐标
int heightH = 60; //IMEI行間距
int imeiCount = 20; //循環IMEI數
int rows = imeiCount / 2; //行數
for (int i = 1; i <= rows; i++)
{
g.DrawImage(image1, 40, heightC + (heightH * i), image1.Width / 2, image1.Height / 2);
g.DrawImage(image1, 220, heightC + (heightH * i), image1.Width / 2, image1.Height / 2);
}
image1.Dispose();
g.Dispose();
//儲存圖檔
image.Save(@"c:\1.jpg");
#region 顯示
//pictureBox1.Image = (Image)image;
//pictureBox1.Image.Save(@"c:\1.jpg");
#endregion
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics; //建立畫闆,這裡的畫闆是由Form提供的.
Pen p = new Pen(Color.Blue, 2);//定義了一個藍色,寬度為的畫筆
g.DrawLine(p, 10, 10, 100, 100);//在畫闆上畫直線,起始坐标為(10,10),終點坐标為(100,100)
g.DrawRectangle(p, 10, 10, 100, 100);//在畫闆上畫矩形,起始坐标為(10,10),寬為,高為
g.DrawEllipse(p, 10, 10, 100, 100);//在畫闆上畫橢圓,起始坐标為(10,10),外接矩形的寬為,高為
/*
Pen p = new Pen(Color.Blue, 5);//設定筆的粗細為,顔色為藍色
Graphics g = this.CreateGraphics();
//畫虛線
p.DashStyle = DashStyle.Dot;//定義虛線的樣式為點
g.DrawLine(p, 10, 10, 200, 10);
//自定義虛線
p.DashPattern = new float[] { 2, 1 };//設定短劃線和空白部分的數組
g.DrawLine(p, 10, 20, 200, 20);
//畫箭頭,隻對不封閉曲線有用
p.DashStyle = DashStyle.Solid;//恢複實線
p.EndCap = LineCap.ArrowAnchor;//定義線尾的樣式為箭頭
g.DrawLine(p, 10, 30, 200, 30);
g.Dispose();
p.Dispose();
*/
/*
Graphics g = this.CreateGraphics();
Rectangle rect = new Rectangle(10, 10, 50, 50);//定義矩形,參數為起點橫縱坐标以及其長和寬
//單色填充
SolidBrush b1 = new SolidBrush(Color.Blue);//定義單色畫刷
g.FillRectangle(b1, rect);//填充這個矩形
//字元串
g.DrawString("字元串", new Font("宋體", 10), b1, new PointF(90, 10));
//用圖檔填充
TextureBrush b2 = new TextureBrush(Image.FromFile(@"e:\picture\1.jpg"));
rect.Location = new Point(10, 70);//更改這個矩形的起點坐标
rect.Width = 200;//更改這個矩形的寬來
rect.Height = 200;//更改這個矩形的高
g.FillRectangle(b2, rect);
//用漸變色填充
rect.Location = new Point(10, 290);
LinearGradientBrush b3 = new LinearGradientBrush(rect, Color.Yellow , Color.Black , LinearGradientMode.Horizontal);
g.FillRectangle(b3, rect);
*/
}
/*
C#仿QQ截圖
接下來看看這是如何做到的.
思路:聊天窗體上有一個截圖按鈕,點選按鈕後,程式将整個螢幕畫在一個新的全屏窗體上,
然後顯示這個窗體.因為是全屏的窗體,并且隐藏了菜單欄、工具欄等,
是以在我們看來就好像是一個桌面的截圖,然後在這個新窗體上畫矩形,
最後儲存矩形中的内容并顯示在原來的聊天窗體中.
步驟:
A.建立一個窗體.命名為Catch.然後設定這個窗體的FormBorderStyle為None,WindowState為Maximized.
B.我們對代碼進行編輯:
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Client
{
public partial class Catch : Form
{
public Catch()
{
InitializeComponent();
}
#region 使用者變量
private Point DownPoint = Point.Empty;//記錄滑鼠按下坐标,用來确定繪圖起點
private bool CatchFinished = false;//用來表示是否截圖完成
private bool CatchStart = false;//表示截圖開始
private Bitmap originBmp;//用來儲存原始圖像
private Rectangle CatchRect;//用來儲存截圖的矩形
#endregion
//窗體初始化操作
private void Catch_Load(object sender, EventArgs e)
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
this.UpdateStyles();
//以上兩句是為了設定控件樣式為雙緩沖,這可以有效減少圖檔閃爍的問題,關于這個大家可以自己去搜尋下
originBmp = new Bitmap(this.BackgroundImage);//BackgroundImage為全屏圖檔,我們另用變量來儲存全屏圖檔
}
//滑鼠右鍵點選結束截圖
private void Catch_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
//滑鼠左鍵按下時動作
private void Catch_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (!CatchStart)
{//如果捕捉沒有開始
CatchStart = true;
DownPoint = new Point(e.X, e.Y);//儲存滑鼠按下坐标
}
}
}
private void Catch_MouseMove(object sender, MouseEventArgs e)
{
if (CatchStart)
{//如果捕捉開始
Bitmap destBmp = (Bitmap)originBmp.Clone();//建立一個圖檔對象,并讓它與原始圖檔相同
Point newPoint = new Point(DownPoint.X, DownPoint.Y);//擷取滑鼠的坐标
Graphics g = Graphics.FromImage(destBmp);//在剛才建立的圖檔上建立一個畫闆
Pen p = new Pen(Color.Blue,1);
int width = Math.Abs(e.X - DownPoint.X), height = Math.Abs(e.Y - DownPoint.Y);//擷取矩形的長和寬
if (e.X < DownPoint.X)
{
newPoint.X = e.X;
}
if (e.Y < DownPoint.Y)
{
newPoint.Y = e.Y;
}
CatchRect = new Rectangle(newPoint,new Size(width,height));//儲存矩形
g.DrawRectangle(p,CatchRect);//将矩形畫在這個畫闆上
g.Dispose();//釋放目前的這個畫闆
p.Dispose();
Graphics g1 = this.CreateGraphics();//重新建立一個Graphics類
//如果之前那個畫闆不釋放,而直接g=this.CreateGraphics()這樣的話無法釋放掉第一次建立的g,因為隻是把位址轉到新的g了.如同string一樣
g1 = this.CreateGraphics();//在整個全屏窗體上建立畫闆
g1.DrawImage(destBmp,new Point(0,0));//将剛才所畫的圖檔畫到這個窗體上
//這個也可以屬于二次緩沖技術,如果直接将矩形畫在窗體上,會造成圖檔抖動并且會有無數個矩形.
g1.Dispose();
destBmp.Dispose();//要及時釋放,不然記憶體将會被大量消耗
}
}
private void Catch_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (CatchStart)
{
CatchStart = false;
CatchFinished = true;
}
}
}
//滑鼠輕按兩下事件,如果滑鼠位于矩形内,則将矩形内的圖檔儲存到剪貼闆中
private void Catch_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left&&CatchFinished)
{
if (CatchRect.Contains(new Point(e.X, e.Y)))
{
Bitmap CatchedBmp = new Bitmap(CatchRect.Width, CatchRect.Height);//建立一個于矩形等大的空白圖檔
Graphics g = Graphics.FromImage(CatchedBmp);
g.DrawImage(originBmp, new Rectangle(0, 0, CatchRect.Width, CatchRect.Height), CatchRect, GraphicsUnit.Pixel);
//把orginBmp中的指定部分按照指定大小畫在畫闆上
Clipboard.SetImage(CatchedBmp);//将圖檔儲存到剪貼闆
g.Dispose();
CatchFinished = false;
this.BackgroundImage = originBmp;
CatchedBmp.Dispose();
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}
}
}
/*
C.建立了Catch窗體後,我們在截圖按鈕(位于聊天窗體上)上加入以下事件:
*/
private void bCatch_Click(object sender, EventArgs e)
{
if (bCatch_HideCurrent.Checked)
{
this.Hide();//隐藏目前窗體
Thread.Sleep(50);//讓線程睡眠一段時間,窗體消失需要一點時間
Catch CatchForm = new Catch();
Bitmap CatchBmp = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);//建立一個和螢幕大小相同的圖檔
Graphics g = Graphics.FromImage(CatchBmp);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height));//儲存全屏圖檔
CatchForm.BackgroundImage = CatchBmp;//将Catch窗體的背景設為全屏時的圖檔
if (CatchForm.ShowDialog() == DialogResult.OK)
{//如果Catch窗體結束,就将剪貼闆中的圖檔放到資訊發送框中
IDataObject iData = Clipboard.GetDataObject();
DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap);
if (iData.GetDataPresent(DataFormats.Bitmap))
{
richtextbox1.Paste(myFormat);
Clipboard.Clear();//清除剪貼闆中的對象
}
this.Show();//重新顯示窗體
}
}
}