• 大小: 59KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-12
  • 语言: C#
  • 标签: c#  串口  图片  格式转换  

资源简介

两个串口一收一发实现图片传输,并且在保存时可以转换成想要的图片格式。包含简单的多线程例子。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Threading;
using System.Drawing.Imaging;

namespace ComPic
{
    public partial class Form1 : Form
    {
        SerialPort sp = new SerialPort();
        bool flag = false;

        SerialPort sp1 = new SerialPort();
        bool flag1 = false;

        string curFileName;
        Bitmap bmp1 = null;
        byte[] filebuff = null;
        byte[] Sendbuff = null;
        byte[] Recvbuff = null;

        Thread thr;
        Thread thr_send;
        bool bln_thr_doflag;

        int total;

       
        //声明一个委托,主要看委托的是什么类型(此处为int)
        public delegate void dlgt(int i);
        public void ChangeBar(int i)
        {
            int sh;
            //progressBar1的InvokeRequired属性判断是否委托,若没有
            //将ChangeBar函数进行委托封装,if条件一般只执行一次,委托一次即可

            //要判断progressBar1有没有被委托做事情,progressBar1放在主线程上
            //已经被ChangeBar函数委托,所以其值要根据ChangeBar(i)传来的值变化
            
            //if (this.label1.InvokeRequired)//设定委托代理
            //两者都可以
            if (this.progressBar1.InvokeRequired)//设定委托代理
            {

                dlgt cg = new dlgt(ChangeBar);
                this.BeginInvoke(cg new object[] { i });
                return;
            }
            else//去修改显示
            {
               sh = (int)((i * 100) / total);
               progressBar1.Value = sh;
               label1.Text = sh.ToString() + “%“ + “    “ + i.ToString() + “/“ + total.ToString();
            }
        }
        void thr_Dosend()
        {
            sp1.Write(Sendbuff 0 Sendbuff.Length);
        }

        //线程函数实体
        void thr_function()
        {

            int i = 0;
            int tmp = 0;

            total = Sendbuff.Length;
            while (bln_thr_doflag)
            {
                tmp = sp.Read(Recvbuff i total - i);
               // tmp = sp.Read(Recvbuff 0 Recvbuff.Length);
                i += tmp;
                if (i >= total)
                    i = total; 
                //在这个线程里,想要根据进度条的速度来改变可视控件label的值
                //需要委托,委托后,在进程中调用可视控件的函数和普通函数一样。
                ChangeBar(i);
                if (i == total)
                    break;
                Thread.Sleep(1);
            }

            MemoryStream blk = new MemoryStream(Recvbuff);
            Image img = Image.FromStream(blk);
            pictureBox2.Image = img;

        }











        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender EventArgs e)
        {
            if (flag == true)
            {
                sp.Close();
                flag = false;

            }
           
         

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        187  2015-12-10 19:42  ComPic\ComPic\App.config

     文件      17920  2015-12-12 11:02  ComPic\ComPic\bin\Debug\ComPic.exe

     文件        187  2015-12-10 19:42  ComPic\ComPic\bin\Debug\ComPic.exe.config

     文件      32256  2015-12-12 11:02  ComPic\ComPic\bin\Debug\ComPic.pdb

     文件      24224  2015-12-12 11:02  ComPic\ComPic\bin\Debug\ComPic.vshost.exe

     文件        187  2015-12-10 19:42  ComPic\ComPic\bin\Debug\ComPic.vshost.exe.config

     文件        490  2013-03-18 17:00  ComPic\ComPic\bin\Debug\ComPic.vshost.exe.manifest

     文件       3792  2015-12-10 19:53  ComPic\ComPic\ComPic.csproj

     文件       9383  2015-12-12 11:02  ComPic\ComPic\Form1.cs

     文件      15797  2015-12-12 10:58  ComPic\ComPic\Form1.Designer.cs

     文件       6596  2015-12-12 10:58  ComPic\ComPic\Form1.resx

     文件        555  2015-12-12 11:02  ComPic\ComPic\obj\Debug\ComPic.csproj.FileListAbsolute.txt

     文件        977  2015-12-12 10:58  ComPic\ComPic\obj\Debug\ComPic.csproj.GenerateResource.Cache

     文件       2211  2015-12-11 09:17  ComPic\ComPic\obj\Debug\ComPic.csprojResolveAssemblyReference.cache

     文件      17920  2015-12-12 11:02  ComPic\ComPic\obj\Debug\ComPic.exe

     文件        180  2015-12-12 10:58  ComPic\ComPic\obj\Debug\ComPic.Form1.resources

     文件      32256  2015-12-12 11:02  ComPic\ComPic\obj\Debug\ComPic.pdb

     文件        180  2015-12-10 19:53  ComPic\ComPic\obj\Debug\ComPic.Properties.Resources.resources

     文件       1453  2015-12-11 09:25  ComPic\ComPic\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6915  2015-12-10 19:43  ComPic\ComPic\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2015-12-10 19:42  ComPic\ComPic\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2015-12-10 19:42  ComPic\ComPic\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2015-12-10 19:42  ComPic\ComPic\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        518  2015-12-10 19:42  ComPic\ComPic\Program.cs

     文件       1336  2015-12-10 19:42  ComPic\ComPic\Properties\AssemblyInfo.cs

     文件       2864  2015-12-10 19:42  ComPic\ComPic\Properties\Resources.Designer.cs

     文件       5612  2015-12-10 19:42  ComPic\ComPic\Properties\Resources.resx

     文件       1093  2015-12-10 19:42  ComPic\ComPic\Properties\Settings.Designer.cs

     文件        249  2015-12-10 19:42  ComPic\ComPic\Properties\Settings.settings

     文件        987  2015-12-10 19:42  ComPic\ComPic.sln

............此处省略12个文件信息

评论

共有 条评论