-
大小: 1.14MB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2023-08-16
- 語(yǔ)言: C#
- 標(biāo)簽: pictureBox??C#??橡皮筋??背景??
資源簡(jiǎn)介
C#的pictureBox上繪圖,帶背景,有橡皮筋效果,實(shí)現(xiàn)畫矩形和直線,圖片可保存。
開發(fā)環(huán)境:Visual Studio 2012(.net framework 4.5)

代碼片段和文件信息
using?System;
using?System.Drawing;
using?System.Windows.Forms;
namespace?WFA畫圖
{
????public?partial?class?Form1?:?Form
????{
????????#region?成員變量
????????Point?m_p1?m_p2;
????????bool?m_flgKeuDowm?=?false;
????????Bitmap?m_mapStart;
????????Bitmap?m_mapEnd;
????????Bitmap?m_mapInit;
????????Graphics?m_graphics;
????????#endregion
????????public?Form1()
????????{
????????????InitializeComponent();
????????????m_graphics?=?pictureBox1.CreateGraphics();
????????????
????????????//?最初的背景圖存起來(lái),清除繪制圖形時(shí)用
????????????m_mapInit?=?(Bitmap)pictureBox1.BackgroundImage;
????????}
????????#region?鼠標(biāo)事件
????????///?
????????///?鼠標(biāo)按下
????????///?
????????///?
????????///?
????????private?void?pictureBox1_MouseDown(object?sender?MouseEventArgs?e)
????????{
????????????m_flgKeuDowm?=?true;
????????????_initPoint(e);
????????????
????????}
????????
????????///?
????????///?鼠標(biāo)移動(dòng)
????????///?
????????///?
????????///?
????????private?void?pictureBox1_MouseMove(object?sender?MouseEventArgs?e)
????????{
????????????if?(!m_flgKeuDowm)
????????????{
????????????????return;
????????????}
????????????else
????????????{
????????????????m_p2?=?new?Point(e.X?e.Y);
????????????}
????????????int?width?=?Math.Abs(e.X?-?m_p1.X);
????????????int?height?=?Math.Abs(e.Y?-?m_p1.Y);
????????????_draw();
????????}
????????///?
????????///?鼠標(biāo)抬起
????????///?
????????///?
????????///?
????????private?void?pictureBox1_MouseUp(object?sender?MouseEventArgs?e)
????????{
????????????m_flgKeuDowm?=?false;
????????????//?最終的圖片設(shè)為背景圖
????????????pictureBox1.BackgroundImage?=?m_mapEnd;
????????????//?起止點(diǎn)初始化
????????????_initPoint(e);
????????}
????????#endregion
????????private?void?_draw(){
????????????//?每次的【終止圖】都是取自【起始圖】
????????????m_mapEnd?=?(Bitmap)m_mapStart.Clone();
????????????Graphics?g?=?Graphics.FromImage(m_mapEnd);
????????????Pen?pen?=?new?Pen(Color.Red3);
????????????if?(rdoRect.Checked)
????????????{
????????????????Point?p1p2;
????????????????_swapPoint(out?p1out?p2?);
????????????????int?width?=?Math.Abs(p2.X?-?p1.X);
????????????????int?height?=?Math.Abs(p2.Y?-?p1.Y);
????????????????g.DrawRectangle(pen?p1.X?p1.Y?width?height);
????????????}else?if(rdoLine.Checked){
????????????????//?畫直線不用轉(zhuǎn)換點(diǎn)坐標(biāo),直接用成員變量的Point
????????????????g.DrawLine(pen?m_p1?m_p2);
????????????}
????????????m_graphics.DrawImage(m_mapEnd?new?Point(0?0));
????????}
????????private?void?_initPoint(MouseEventArgs?e)
????????{
????????????m_p1?=?new?Point(e.X?e.Y);
????????????m_p2?=?m_p1;
????????????if?(pictureBox1.BackgroundImage?!=?null)
????????????{
????????????????m_mapStart?=?(Bitmap)pictureBox1.BackgroundImage;
????????????}
????????}
????????private?void?
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????917??2018-06-16?20:01??WFA畫圖.sln
????..A..H.?????26112??2018-06-16?23:32??WFA畫圖.v11.suo
?????文件????????187??2018-06-16?20:01??WFA畫圖\App.config
?????文件?????297984??2018-06-17?00:03??WFA畫圖\bin\Debug\WFA畫圖.exe
?????文件????????187??2018-06-16?20:01??WFA畫圖\bin\Debug\WFA畫圖.exe.config
?????文件??????30208??2018-06-17?00:03??WFA畫圖\bin\Debug\WFA畫圖.pdb
?????文件??????22984??2018-06-17?00:04??WFA畫圖\bin\Debug\WFA畫圖.vshost.exe
?????文件????????187??2018-06-16?20:01??WFA畫圖\bin\Debug\WFA畫圖.vshost.exe.config
?????文件????????490??2016-07-16?19:44??WFA畫圖\bin\Debug\WFA畫圖.vshost.exe.manifest
?????文件???????4491??2018-06-17?00:03??WFA畫圖\Form1.cs
?????文件???????5352??2018-06-17?00:03??WFA畫圖\Form1.Designer.cs
?????文件???????5817??2018-06-17?00:03??WFA畫圖\Form1.resx
?????文件???????7348??2018-06-16?23:07??WFA畫圖\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件??????????0??2018-06-16?20:01??WFA畫圖\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件??????????0??2018-06-16?20:01??WFA畫圖\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件??????????0??2018-06-16?20:01??WFA畫圖\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
?????文件???????5120??2018-06-16?23:07??WFA畫圖\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll
?????文件???????1059??2018-06-17?00:04??WFA畫圖\obj\Debug\WFA畫圖.csproj.FileListAbsolute.txt
?????文件???????1061??2018-06-17?00:03??WFA畫圖\obj\Debug\WFA畫圖.csproj.GenerateResource.Cache
?????文件???????2209??2018-06-16?22:56??WFA畫圖\obj\Debug\WFA畫圖.csprojResolveAssemblyReference.cache
?????文件?????297984??2018-06-17?00:03??WFA畫圖\obj\Debug\WFA畫圖.exe
?????文件????????180??2018-06-17?00:03??WFA畫圖\obj\Debug\WFA畫圖.Form1.resources
?????文件??????30208??2018-06-17?00:03??WFA畫圖\obj\Debug\WFA畫圖.pdb
?????文件?????286159??2018-06-16?23:08??WFA畫圖\obj\Debug\WFA畫圖.Properties.Resources.resources
?????文件????????521??2018-06-16?20:01??WFA畫圖\Program.cs
?????文件???????1338??2018-06-16?20:01??WFA畫圖\Properties\AssemblyInfo.cs
?????文件???????3610??2018-06-16?23:07??WFA畫圖\Properties\Resources.Designer.cs
?????文件???????6453??2018-06-16?23:07??WFA畫圖\Properties\Resources.resx
?????文件???????1096??2018-06-16?20:01??WFA畫圖\Properties\Settings.Designer.cs
?????文件????????249??2018-06-16?20:01??WFA畫圖\Properties\Settings.settings
............此處省略14個(gè)文件信息
評(píng)論
共有 條評(píng)論