最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用C#實(shí)現(xiàn)一個(gè)簡單的繪圖工具

 更新時(shí)間:2024年02月01日 10:57:09   作者:lingxiao16888  
這篇文章主要為大家詳細(xì)介紹了如何使用C#開發(fā)的簡單繪圖工具,可以將簽名簡單繪圖后的效果以圖片的形式導(dǎo)出,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1.目的

使用C#開發(fā)的簡單繪圖工具,將簽名,簡單繪圖后的效果以圖片的形式導(dǎo)出。

2.相關(guān)技術(shù)

GDI.....實(shí)在沒啥

3.效果展示

無背景狀態(tài):

導(dǎo)出的圖片效果:

添加背景后:

導(dǎo)出的圖片效果:

4.代碼

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            g = this.CreateGraphics();
        }
        Graphics g;
        Graphics imgGraphice;
        Color curColor = Color.Black;
        string title;
        Image backImg;
        DrawingMode curDrawingTool = DrawingMode.None;
        int penSize = 0;
        bool drawingFlag = false;
        TextBox tb;
        private void Form1_Load(object sender, EventArgs e)
        {
            title = this.Text;
            toolStripComboBox1.SelectedIndex = 0;
        }
 
        private void opentoolStrip_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "jpg圖片|*.jpg";
                ofd.Multiselect = false;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    Image sourceImage = Image.FromFile(ofd.FileName);                   
                    backImg = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
                    imgGraphice = Graphics.FromImage(backImg);
                    RectangleF rec = new RectangleF(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);
                    imgGraphice.DrawImage(sourceImage,rec  , new RectangleF(0, 0, sourceImage.Width, sourceImage.Height), GraphicsUnit.Pixel);
                   
                     g.Clear(Color.White);                  
                    g.DrawImage(sourceImage,this.ClientRectangle);
                    this.Text = title + "\t" + ofd.FileName;
                }
 
            }
        }
 
        private void newtoolStrip_Click(object sender, EventArgs e)
        {
            //自定義圖片
            backImg = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            imgGraphice = Graphics.FromImage(backImg);
            imgGraphice.Clear(Color.White);
            g.DrawImage(backImg, this.ClientRectangle);
            this.Text = title + "\t未命名";
        }
 
        private void savetoolStrip_Click(object sender, EventArgs e)
        {
            if (backImg == null) return;
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.Filter = "圖像文件|*.jpg";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    backImg.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                    MessageBox.Show("保存至:" + sfd.FileName, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
 
        private void exittoolStrip_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
 
        private void colortoolStrip_Click(object sender, EventArgs e)
        {
            using (ColorDialog cd = new ColorDialog())
            {
                if (cd.ShowDialog() == DialogResult.OK)
                {
                    curColor = cd.Color;
                }
            }
        }
 
        private void btnPen_Click(object sender, EventArgs e)
        {
            curDrawingTool = DrawingMode.Pen;
        }
 
        private void btnLine_Click(object sender, EventArgs e)
        {
            curDrawingTool = DrawingMode.Line;
        }
 
        private void btnRec_Click(object sender, EventArgs e)
        {
            curDrawingTool = DrawingMode.Rectangle;
        }
 
        private void btnEllipse_Click(object sender, EventArgs e)
        {
            curDrawingTool = DrawingMode.Ellipse;
        }
 
        private void btnText_Click(object sender, EventArgs e)
        {
            curDrawingTool = DrawingMode.Text;
        }
 
        private void btnRubber_Click(object sender, EventArgs e)
        {
            curDrawingTool = DrawingMode.Rubber;
        }
 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //進(jìn)行重繪不然窗口最小化后就沒有
            if (backImg != null)
                g.DrawImage(backImg, this.ClientRectangle);
        }
        Point oldPoint;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                oldPoint = e.Location;
                drawingFlag = true;
            }
            switch (curDrawingTool)
            {
                case DrawingMode.None:
                    break;
                case DrawingMode.Pen:
                    break;
                case DrawingMode.Line:
                    break;
                case DrawingMode.Rectangle:
                    break;
                case DrawingMode.Ellipse:
                    break;
                case DrawingMode.Text:
                    if (imgGraphice == null)
                        return;
                    //在當(dāng)前位置添加
                    tb = new TextBox();
                    tb.Width = 100;
                    tb.Height = 30;
                    tb.BackColor = Color.FromArgb(192, 255, 192);
                    tb.BorderStyle = BorderStyle.Fixed3D;
                    tb.Location = e.Location;
                    tb.KeyDown += Tb_KeyDown;
                    this.Controls.Add(tb);
 
                    break;
                case DrawingMode.Rubber:
                    break;
                default:
                    break;
            }
        }
 
        private void Tb_KeyDown(object sender, KeyEventArgs e)
        {
            if (imgGraphice == null)
                return;
            if (e.KeyValue == 13)
            {
                g.DrawString(tb.Text, new Font("宋體", 12), new SolidBrush(curColor), oldPoint);
                imgGraphice.DrawString(tb.Text, new Font("宋體", 12), new SolidBrush(curColor), oldPoint);
                this.Controls.Remove(tb);
            }
            
        }
 
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            labCoord.Text =  e.Location.ToString();
            if (imgGraphice == null || drawingFlag == false) return;
            switch (curDrawingTool)
            {
                case DrawingMode.None:
                    break;
                case DrawingMode.Pen:
                    
                    g.DrawLine(new Pen(curColor, penSize), oldPoint, e.Location);
                    imgGraphice.DrawLine(new Pen(curColor, penSize), oldPoint, e.Location);
                    oldPoint = e.Location;
                    break;
                case DrawingMode.Line:
                    //為表現(xiàn)出實(shí)時(shí)效果需要擦寫痕跡效果(即在form上進(jìn)行繪制只是表象,一切以在image上繪制為準(zhǔn))
                    Form1_Paint(null, null);
                    g.DrawLine(new Pen(curColor, penSize), oldPoint, e.Location);
                    break;
                case DrawingMode.Rectangle:
                    Form1_Paint(null, null);
                    g.DrawRectangle(new Pen(curColor, penSize),oldPoint.X,oldPoint.Y,e.X-oldPoint.X,e.Y-oldPoint.Y);
                    break;
                case DrawingMode.Ellipse:
                    Form1_Paint(null, null);
                    g.DrawEllipse(new Pen(curColor, penSize), oldPoint.X, oldPoint.Y, e.X - oldPoint.X, e.Y - oldPoint.Y);
                    break;
                case DrawingMode.Text:
                    break;
                case DrawingMode.Rubber:
 
                    //所謂橡皮擦就是繪制白線
                    g.DrawLine(new Pen(Color.White, penSize), oldPoint, e.Location);
                    imgGraphice.DrawLine(new Pen(Color.White, penSize), oldPoint, e.Location);
                    oldPoint = e.Location;
                    break;
                default:
                    break;
            }
        }
 
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            drawingFlag = false;
            if (imgGraphice == null) return;
            switch (curDrawingTool)
            {
                case DrawingMode.None:
                    break;
                case DrawingMode.Pen:
                    break;
                case DrawingMode.Line:
                    //這里繪制Line的最終效果
                    imgGraphice.DrawLine(new Pen(curColor, penSize), oldPoint, e.Location);
                    break;
                case DrawingMode.Rectangle:
                   imgGraphice.DrawRectangle(new Pen(curColor, penSize), oldPoint.X, oldPoint.Y, e.X - oldPoint.X, e.Y - oldPoint.Y);
                    break;
                case DrawingMode.Ellipse:
                    imgGraphice.DrawEllipse(new Pen(curColor, penSize), oldPoint.X, oldPoint.Y, e.X - oldPoint.X, e.Y - oldPoint.Y);
                    break;
                case DrawingMode.Text:
                    break;
                case DrawingMode.Rubber:
                    break;
                default:
                    break;
            }
        }
 
        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int size;
            if (int.TryParse(toolStripComboBox1.Text, out size))
            {
                penSize = size;
            }
 
        }
    }
    enum DrawingMode
    {
        None,
        Pen,
        Line,
        Rectangle,
        Ellipse,
        Text,
        Rubber
    }

到此這篇關(guān)于使用C#實(shí)現(xiàn)一個(gè)簡單的繪圖工具的文章就介紹到這了,更多相關(guān)C#繪圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#實(shí)現(xiàn)壓縮圖片為可控制的JPEG格式

    C#實(shí)現(xiàn)壓縮圖片為可控制的JPEG格式

    這篇文章主要為大家詳細(xì)介紹了使用C#實(shí)現(xiàn)將圖片壓縮為質(zhì)量可自己控制的JPEG的幾種方式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2024-01-01
  • C#使用linq給List某個(gè)屬性值賦值方式

    C#使用linq給List某個(gè)屬性值賦值方式

    在C#中使用LINQ進(jìn)行數(shù)據(jù)查詢與修改的最佳實(shí)踐,推薦使用ForEach方法或傳統(tǒng)的foreach循環(huán)進(jìn)行List對象屬性賦值;避免直接在LINQ查詢表達(dá)式中執(zhí)行賦值操作;可考慮使用Select投影生成新列表以保持?jǐn)?shù)據(jù)不變性
    2026-06-06
  • C#使用自定義算法對數(shù)組進(jìn)行反轉(zhuǎn)操作的方法

    C#使用自定義算法對數(shù)組進(jìn)行反轉(zhuǎn)操作的方法

    這篇文章主要介紹了C#使用自定義算法對數(shù)組進(jìn)行反轉(zhuǎn)操作的方法,涉及C#針對數(shù)組操作的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C#實(shí)現(xiàn)的優(yōu)酷真實(shí)視頻地址解析功能(2014新算法)

    C#實(shí)現(xiàn)的優(yōu)酷真實(shí)視頻地址解析功能(2014新算法)

    這篇文章主要介紹了C#實(shí)現(xiàn)的優(yōu)酷真實(shí)視頻地址解析功能(2014新算法),本文在當(dāng)前環(huán)境下是有效的,因?yàn)閮?yōu)酷之前更新了算法,需要的朋友可以參考下
    2014-10-10
  • C#并行庫Task類介紹

    C#并行庫Task類介紹

    這篇文章介紹了C#并行庫Task類,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#線程池用法詳細(xì)介紹

    C#線程池用法詳細(xì)介紹

    在C#編程語言中,使用線程池可以并行地處理工作,當(dāng)強(qiáng)制線程和更新進(jìn)度條時(shí),會使用內(nèi)建架構(gòu)的ThreadPool類,為批處理使用多核結(jié)構(gòu),這里我們來看在C#編程語言中一些關(guān)于來自System.Threading的ThreadPool的用法的例子
    2013-11-11
  • C#使用GUID(全局統(tǒng)一標(biāo)識符)

    C#使用GUID(全局統(tǒng)一標(biāo)識符)

    這篇文章介紹了C#使用GUID(全局統(tǒng)一標(biāo)識符)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#子線程更新UI控件的方法實(shí)例總結(jié)

    C#子線程更新UI控件的方法實(shí)例總結(jié)

    這篇文章主要介紹了C#子線程更新UI控件的方法,在桌面應(yīng)用程序中控制UI界面有著不錯(cuò)的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-09-09
  • C#多線程爬蟲抓取免費(fèi)代理IP的示例代碼

    C#多線程爬蟲抓取免費(fèi)代理IP的示例代碼

    本篇文章主要介紹了C#多線程爬蟲抓取免費(fèi)代理IP的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • C#實(shí)現(xiàn)百度ping推送功能的方法

    C#實(shí)現(xiàn)百度ping推送功能的方法

    百度ping是網(wǎng)站優(yōu)化必做的事情,這樣才能主動(dòng)推送給百度,那么基于代碼是如何實(shí)現(xiàn)百度推送方法呢?下文小編給大家?guī)砹薈#實(shí)現(xiàn)百度ping推送功能的方法,非常不錯(cuò),感興趣的朋友一起學(xué)習(xí)吧
    2016-08-08

最新評論

清水河县| 霍邱县| 兴化市| 宾川县| 右玉县| 大洼县| 大余县| 兴义市| 高雄县| 手游| 临汾市| 永登县| 察哈| 南投县| 武功县| 九龙城区| 抚顺市| 涟水县| 荣成市| 交口县| 博乐市| 旬阳县| 炉霍县| 新干县| 泗水县| 道真| 台湾省| 金堂县| 公主岭市| 承德市| 禹州市| 台中市| 射阳县| 宁河县| 隆安县| 寿阳县| 兖州市| 舟山市| 林芝县| 南城县| 鞍山市|