資源簡介
兩個串口一收一發實現圖片傳輸,并且在保存時可以轉換成想要的圖片格式。包含簡單的多線程例子。

代碼片段和文件信息
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個文件信息
評論
共有 條評論