存储的图片读取为数据流

private void btnSelectImage_Click(object sender, EventArgs e)
{
//将需要存储的图片读取为数据流
openFileDialog1.Filter = "*jpg|*jpg|*bmp|*bmp";
if (DialogResult.OK == openFileDialog1.ShowDialog())
{


foreach (string file in openFileDialog1.FileNames)
{
FileStream fsBLOBFile = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] bytBLOBData = new byte[fsBLOBFile.Length];
buffer = new byte[fsBLOBFile.Length];
fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
fsBLOBFile.Close();
buffer = bytBLOBData;
}
}
}


void ShowImage(byte[] imagecode)
{
if (imagecode != null)
{
MemoryStream stmBLOBData = new MemoryStream(imagecode);
System.Drawing.Image bmp = System.Drawing.Image.FromStream(stmBLOBData);
Rectangle rc = pictureBox1.ClientRectangle;
SizeF size = new SizeF(bmp.Width / bmp.HorizontalResolution, bmp.Height / bmp.VerticalResolution);
float fScale = Math.Min(rc.Width / size.Width, rc.Height / size.Height);
size.Width *= fScale;
size.Height *= fScale;
pictureBox1.Image = new Bitmap(bmp, size.ToSize());
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
stmBLOBData.Close();
}
}

你可能感兴趣的:(图片)