Sunteți pe pagina 1din 4

Código para colocar imagens (objectos OLE) existentes numa base de dados -

ACCESS

Para mostrar o conteúdo na picturebox

SqlConnection cn = new SqlConnection(strCn);


cn.Open();

//Retrieve BLOB from database into DataSet.


SqlCommand cmd = new SqlCommand("SELECT BLOBID, BLOBData FROM BLOBTest
ORDER BY BLOBID", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "BLOBTest");
int c = ds.Tables["BLOBTest"].Rows.Count;

Byte[] byteBLOBData = new Byte[0];


byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image= Image.FromStream(stmBLOBData);

Para guardar as imagens na base dados

SqlConnection cn = new SqlConnection(strCn);


SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES
(@BLOBData)", cn);

//Save image from PictureBox into MemoryStream object.


MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);

//Read from MemoryStream into Byte array.


Byte[] bytBLOBData = new Byte[ms.Length];
ms.Position = 0;
ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length));
//Create parameter for insert statement that contains image.
SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary,
bytBLOBData.Length, ParameterDirection.Input, false, 0, 0,null,
DataRowVersion.Current, bytBLOBData);
cmd.Parameters.Add(prm);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();

Para guardar as imagens na base dados, a partir de imagens existentes no


disco

//Declare the OpenFileDialog as a new OpenFileDialog.
OpenFileDialog openFileDialog = new OpenFileDialog();
//Declare the PictureBox as a new PictureBox
//PictureBox pic = new PictureBox();
//Set the openFileDialog's Filter.
openFileDialog.Filter = "Bitmap Image|*.bmp|All Files|*.*";
//Set the openFileDialog's FileName to nothing.
openFileDialog.FileName = "";
string path = "";
//Show the openFileDialog
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
    //To do code if the Open button was pressed
    //Use something like:
    filename = openFileDialog.FileName;
    pic.Image = Image.FromFile(filename);
    pic.Size = new System.Drawing.Size(280, 140);
    pic.SizeMode = PictureBoxSizeMode.CenterImage;
   path = System.IO.Path.GetDirectoryName(openFileDialog.FileName) + "
\\" + openFileDialog.FileName;
    FullFileName = openFileDialog.FileName; 
    txtComp_Image.Text = path;
 
 
}
else
{
    return;
}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

byte[] ImageByte = null;


MemoryStream MemStream = null;
PictureBox PicBx = new PictureBox();
string WorkingDirectory = Application.StartupPath + "\\";
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + WorkingDirectory + "DBFile.mdb;
Persist Security Info=True";
cnction = new OleDbConnection(connString);
cnction.Open();
int ImageID = 6;
sqlCommand = "SELECT ImageObject FROM ImagesTable WHERE ImageID = " + ImageID + "";
comm = new OleDbCommand(sqlCommand, cnction);
ImageByte = comm.ExecuteScalar();
MemStream = new MemoryStream(ImageByte);
PicBx.Image =Image.FromStream(MemStream);

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

It works in my code-

CImageHandling objPhoto = new CImageHandling();

byte[] MyData = new byte[0];

MyData = (byte[])dt.Rows[0]["fldPhotoSignature"];

int ArraySize = new int();

ArraySize = MyData.GetUpperBound(0);

MyPic.Image = objPhoto.GetImageData(MyData);

---------------

public Image GetImageData(byte[] ImageByte)

System.IO.MemoryStream mst = new System.IO.MemoryStream(ImageByte, 0,


ImageByte.Length);

mst.Write(ImageByte, 0, ImageByte.Length);

System.Drawing.Image newImage;

newImage = System.Drawing.Image.FromStream(mst);

return newImage;

IMPORTANTE
So far the only way I have been able to get these samples to work (and the one above)
is by excluding the OLE Object header information. 
            System.IO.MemoryStream mst = new System.IO.MemoryStream(ImageByte, 78,
ImageByte.Length-78);
            mst.Write(ImageByte, 78, ImageByte.Length-78);

S-ar putea să vă placă și