当前位置:编程学习 > C#/ASP.NET >>

存储的图片读取为数据流

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();
            }
        }


作者:chinaboykai
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,