C# 使用Base64編碼實現圖片的上傳


效果圖

 

 代碼如下

//選擇圖片

OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "選擇要上傳的圖片";
ofd.Filter = "All Files(*.*)|*.*|位圖(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg";
ofd.ShowDialog();
textBox1.Text = ofd.FileName;
if (!File.Exists(ofd.FileName))
{
MessageBox.Show("???");
return;
}

////////////

//上傳圖片

string strPath = textBox1.Text.Trim();
System.IO.MemoryStream m = new System.IO.MemoryStream();
System.Drawing.Bitmap bp = new System.Drawing.Bitmap(strPath);
bp.Save(m, System.Drawing.Imaging.ImageFormat.Png);
byte[] b = m.GetBuffer();
string base64string = Convert.ToBase64String(b);
//自己寫入庫操作

............

 

//////////

//顯示圖片    解碼

//查詢操作

............

byte[] imageBytes=Convert.FromBase64String(base編碼);

MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);

memoryStream.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(memoryStream);
this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
this.pictureBox1.Image = image;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM