//PictureBox控件中图片Base64字符串 [可用作串口传输图片]
private string BmpToBase64(PictureBox Picbox)
{
string Base64str;
using (MemoryStream stream = new MemoryStream())
{
Picbox.Image.Save(stream, ImageFormat.Jpeg);//图像控件中图像存为流,并定义格式
byte[] by = stream.ToArray();//转成二进制
Base64str = System.Convert.ToBase64String(by);//转64位
}
return Base64str;
}
//64位字符转图片
private Image Base64ToBmp(String str)
{
byte[] b = Convert.FromBase64String(str);//逆base64
MemoryStream ms = new MemoryStream(b);
Bitmap bmp = new Bitmap(ms);
//using (MemoryStream stream = new MemoryStream(b))
//{
// Image img = Image.FromStream(stream);
// pictureBox1.Image = img;
//}
return bmp;
}
//截图只存控件部分
this.button1.Visible = false;//截图不显示部分
this.button2.Visible = false;
this.button3.Visible = false;
textBox1.Focus();//不显示焦点
Application.DoEvents();//让界面不受延时影响
//----------------------------------------------------------------------------------------
Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
Graphics g = Graphics.FromImage(bit);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;//质量设为最高
// g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
g.CopyFromScreen(panel1.PointToScreen(Point.Empty), Point.Empty, panel1.Size);//只保存某个控件
bit.Save($"{Application.StartupPath}\\Doc\\{label2.Text}.Png");//默认保存格式为PNG,保存成jpg格式质量不是很好