private void link_save_img_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { Bitmap imageBitMap = null; string base64 = text_base64_box.Text; if (!String.IsNullOrEmpty(base64)) { // 优先保存base64原图 imageBitMap = ImageBase64Utils.toImage(base64); } else if (picture_box.Image != null) { // 其次保存pic控件上的图 imageBitMap = new Bitmap(picture_box.ClientRectangle.Width, picture_box.ClientRectangle.Height); picture_box.DrawToBitmap(imageBitMap, picture_box.ClientRectangle); } if (imageBitMap == null) { MessageBox.Show("没有图片可以保存。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } SaveFileDialog save = new SaveFileDialog(); save.FileName = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now); save.Filter = "(.png)|*.png"; if (save.ShowDialog() == DialogResult.OK) { //GDI+ 中发生一般性错误解决方案:将imageBitMap拷贝到tempBitmap中 Bitmap tempBitmap = new Bitmap(imageBitMap.Width, imageBitMap.Height); Graphics draw = Graphics.FromImage(tempBitmap); draw.DrawImage(imageBitMap, 0, 0); save.Dispose(); draw.Dispose(); imageBitMap.Dispose(); tempBitmap.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Jpeg); setStatusText("图片已保存——>" + save.FileName); } }
private void btn_base64_to_img_Click(object sender, EventArgs e) { string base64 = text_base64_box.Text; if (String.IsNullOrEmpty(base64)) { MessageBox.Show("请先填写Base64编码。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Bitmap bitmap = ImageBase64Utils.toImage(base64); if (bitmap != null) { picture_box.Image = bitmap; } setStatusText("Base64编码 ——> 图片 已完成"); }
private void btn_img_to_base64_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Multiselect = false; dlg.Title = "选择要转换的图片"; dlg.Filter = "图像文件(*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"; if (DialogResult.OK == dlg.ShowDialog()) { string imageFilePath = dlg.FileName; picture_box.ImageLocation = imageFilePath; String base64 = ImageBase64Utils.toBase64(imageFilePath); if (!String.IsNullOrEmpty(base64)) { text_base64_box.Clear(); text_base64_box.Text = base64; } } dlg.Dispose(); setStatusText("图片 ——> Base64编码 已完成"); }