从数据库检索图像

从数据库检索图像

本文介绍了从数据库检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DigiAlert1
{
    public partial class converting : Form
    {
        SqlConnection con = new SqlConnection("server=RND3-12\\MSSQLEXPDB;user id=sa;password=prayoglabs@123;database=mlrit");
        SqlDataAdapter da;
        DataSet ds;
        string s;
        MemoryStream ms;
        FileStream fs;
        Byte[] b;
        string fname;
        public converting()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            pictureBox1.ImageLocation = openFileDialog1.FileName;
            s = openFileDialog1.FileName;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //FileInfo file = new FileInfo(s);
            //FileStream fs = new FileStream(s, FileMode.Open, FileAccess.Read);
            //BinaryReader br = new BinaryReader(fs);
            //byte[] image = br.ReadBytes((int)fs.Length);
            //br.Close();
            //fs.Close();
            //da = new SqlDataAdapter("insert into img values(''" + image + "'')", con);
            //ds = new DataSet();
            //da.Fill(ds, "img");
            updatedata();
        }
        private void updatedata()
        {
            //use filestream object to read the image.
            //read to the full length of image to a byte array.
            //add this byte as an oracle parameter and insert it into database.
            try
            {
                //proceed only when the image has a valid path
                if (s != "")
                {
                    FileStream fs;
                    fs = new FileStream(s, FileMode.Open, FileAccess.Read);
                    //a byte array to read the image
                    byte[] picbyte = new byte[fs.Length];
                    fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
                    fs.Close();
                    da = new SqlDataAdapter("insert into img values(''" + picbyte + "'')", con);
                    ds = new DataSet();
                    da.Fill(ds, "img");
                    MessageBox.Show("Image Added");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //private void button3_Click(object sender, EventArgs e)
        //{
        //    da = new SqlDataAdapter("select * from img where id=107", con);
        //    ds = new DataSet();
        //    da.Fill(ds, "img");
        //    byte[] data = (byte[])ds.Tables["img"].Rows[0][1];
        //    ms = new MemoryStream(data, true);
        //    var i = ms;
        //    // textBox2.Text = ms.Length.ToString();
        //    pictureBox2.Image = Image.FromStream(ms);
        //    ms.Write(data, 0, Convert.ToInt32(data.Length));
        //    // ms.Write(data, 0, data.Length);
        //    // tempImg = Image.FromStream(ms);
        //    Image timg = pictureBox2.Image;
        //    pictureBox2.Image = timg;
        //    pictureBox2.Refresh();
        // }

        private void button3_Click(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from img where id=104", con);
            ds = new DataSet();
            da.Fill(ds, "img");
            FileStream FS1 = new FileStream("image.jpg", FileMode.Create);
            byte[] blob = (byte[])ds.Tables["img"].Rows[0][1];
            FS1.Write(blob, 0, blob.Length);
            FS1.Close();
            FS1 = null;
            pictureBox2.Image = Image.FromFile("image.jpg");
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox2.Refresh();
        }
            //private void button2_Click(object sender, EventArgs e)
            //{
            //    ms = new MemoryStream();
            //    fs = new FileStream(fname, FileMode.Open, FileAccess.Read);
            //    b = new Byte[fs.Length];
            //    fs.Read(b, 0, Convert.ToInt32(fs.Length));
            //    fs.Close();
            //    pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
            //    byte[] data = new byte[ms.Length];
            //    ms.Position = 0;
            //    var i = ms.Read(data, 0, Convert.ToInt32(ms.Length));
            //    da = new SqlDataAdapter("insert into img values(''" + i + "'')", con);
            //    ds = new DataSet();
            //    da.Fill(ds, "img");
            //    MessageBox.Show("success");
            //}

    }
}

推荐答案

public byte[] ReadFile(string ImagePath)
       {
           byte[] Image_Binary_data = null;
           FileInfo FInfo = new FileInfo(ImagePath);
           long numBytes = FInfo.Length;
           FileStream FStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
           BinaryReader BR = new BinaryReader(FStream);
           Image_Binary_data = BR.ReadBytes((int)numBytes);
           return Image_Binary_data;
       }




下面的代码将图片显示到图片框中...




below code dispalys the picture into picture box...

byte[] ImageData = binary_data_stored_into_database;

            Image NewImage;
            using (MemoryStream MS = new MemoryStream(ImageData, 0, ImageData.Length))
            {
                MS.Write(ImageData, 0, ImageData.Length);
                NewImage = Image.FromStream(MS, true);
            }

            pic_image.Image = NewImage;


这篇关于从数据库检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:39