Displaying multiple image retrieving from database - c#

My project allow admin to add medal for officer profile, currently I only able to insert the maximum of 5 medals. But my teacher ask me to let the admin insert as many medal as they want for the officer profile. I not sure how do I retrieve all the image that the admin inserted, I know how to insert image into database using varbinary. Do give me way for doing this. THANKS!
Code Below is how I do for inserting at maximum of 5 medals:
Code for uploading:
System.Drawing.Image uploaded = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
System.Drawing.Image newImage = new Bitmap(1024, 768);
using (Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(uploaded, 0, 0, 1024, 768);
}
byte[] results;
using (MemoryStream ms = new MemoryStream())
{
ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
EncoderParameters jpegParms = new EncoderParameters(1);
jpegParms.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
newImage.Save(ms, codec, jpegParms);
results = ms.ToArray();
}
string sqlImage = "Insert into OfficerMedal(policeid, image) values ('" + Session["policeid"] + "', #Data)";
SqlCommand cmdImage = new SqlCommand(sqlImage);
cmdImage.Parameters.AddWithValue("#Data", results);
InsertUpdateData(cmdImage);
I retrieve image using aspx page
protected void Page_Load(object sender, EventArgs e)
{
string strQuery = "select image from OfficerMedal where policeid='" + Session["policeid"] + "'";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
if (dt != null)
{
download(dt);
}
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch
{
return null;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
private void download(DataTable dt)
{
// check if you have any rows at all
// no rows -> no data to convert!
if (dt.Rows.Count <= 0)
return;
// check if your row #0 even contains data -> if not, you can't do anything!
if (dt.Rows[0].IsNull("image"))
return;
Byte[] bytes = (Byte[])dt.Rows[0]["image"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpg";
Response.BinaryWrite(bytes);
Response.End();
}
To add on for this current method I retrieving image 1 by 1 from database. Instead of retrieving all image that belong to the officer.

The usual way to do this is with a HttpHandler rather than in your ASPX page itself. The handler is responsible for retrieving the image data from your database and outputting it as a graphic; it can then be used as the src for a regular <img> tag or the ImageUrl property for an <asp:image> control.
The easiest way to add a handler is to select Generic Handler in the Add New Item dialog:
In the code for the handler, ProcessRequest is the method that does the work e.g.
public void ProcessRequest(HttpContext context)
{
byte[] imageBytes;
// Get the id of the image we want to show
string imageId = context.Request.QueryString["ImageId"];
// Get the image bytes out of the database
using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
// Pass in the image id we got from the querystring
SqlCommand cmd = new SqlCommand("SELECT image FROM PoliceMedal WHERE ImageId=" + imageId, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
reader.GetBytes(0, 0, imageBytes, 0, int.MaxValue);
}
context.Response.Buffer = true;
context.Response.Charset = "";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = "image/jpg";
context.Response.BinaryWrite(imageBytes);
context.Response.End();
}
So we have our handler that will return the image data from the database, and we call this with a url like \ImageHandler.ashx?ImageId=1. This does require a change to your database in that you'll need to add a key to the PoliceMedal table - I suggest a SQL Server IDENTITY column.
It's not clear from your question how you're displaying the images on the ASPX page, so here I'll just show you how to add them into a PlaceHolder. So, we'll retrieve the set of image ids for an officer, construct an <asp:image> control for each of them and add the images to the PlaceHolder.
protected void Page_Load(object sender, EventArgs e)
{
using (
SqlConnection conn =
new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
// Query the table to get the list of image IDs for the current user
SqlCommand cmd = new SqlCommand("SELECT ImageId FROM PoliceMedal WHERE policeid = " + Session["policeid"], conn);
conn.Open();
SqlDataReader imageReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
// For each image in the database
while (imageReader.Read())
{
// Create the new Image control
Image medalImage = new Image();
// Call the handler, passing the id of the image in the querystring
medalImage.ImageUrl = string.Format("ImageHandler.ashx?ImageId={0}",
imageReader.GetInt32(0));
// Add the image to the placeholder
MedalPlaceHolder.Controls.Add(medalImage);
}
}
}

Related

C# Read the image from database and Save it to another database

Below this code is where i try to retrieve the image to my database and try to save it to another database. In the first image you will see the error that i encountered during the run time when i click that ok button. In the second image you will see that i already inserted the data with my retrieve image but i encountered that error (I see that the image is inserted but i don't know if that is the correct one)
In my employee_product table i have the "Image" column = image datatype
(This table is where i retrieve my image)
In my product_result table I have the same column and same datatype
(This table is where i inserted the retrieved image)
What i want to happen is to retrieve that image and when click the ok button it will save to my database also it will show to my datagridview
//My Code for Adding the image to my employee_product
private void btn_add_Click(object sender, EventArgs e)
{
using (var con = SQLConnection.GetConnection())
{
try
{
var ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
byte[] PhotoByte = ms.ToArray();
if (string.IsNullOrEmpty(cbox_supplier.Text) || string.IsNullOrEmpty(txt_code.Text) || string.IsNullOrEmpty(txt_item.Text) || string.IsNullOrEmpty(txt_quantity.Text) || string.IsNullOrEmpty(txt_cost.Text))
{
MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
var selectCommand = new SqlCommand("Insert into employee_product (Image, Supplier, Codeitem, Itemdescription, Date, Quantity, Unitcost) Values (#Image, #Supplier, #Codeitem, #Itemdescription, #Date, #Quantity, #Unitcost)", con);
selectCommand.Parameters.AddWithValue("#Image", PhotoByte);
selectCommand.Parameters.AddWithValue("#Supplier", cbox_supplier.Text);
selectCommand.Parameters.AddWithValue("#Codeitem", txt_code.Text.Trim());
selectCommand.Parameters.AddWithValue("#Itemdescription", txt_item.Text.Trim());
selectCommand.Parameters.AddWithValue("#Date", date);
selectCommand.Parameters.AddWithValue("#Quantity", txt_quantity.Text.Trim());
selectCommand.Parameters.AddWithValue("#Unitcost", txt_cost.Text.Trim());
selectCommand.ExecuteNonQuery();
MessageBox.Show("Added successfully", "SIMS", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
txt_path.Clear();
pictureBox1.Image = null;
cbox_supplier.Items.Clear();
txt_code.Clear();
txt_item.Clear();
txt_quantity.Clear();
txt_cost.Clear();
_view.AddingProduct();
}
}
catch (Exception)
{
MessageBox.Show("Input image");
}
}
}
byte[] data;
public void display()
{
using (var con = SQLConnection.GetConnection())
{
SqlCommand selectTable = new SqlCommand("Select * from product_result ; ", con);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = selectTable;
dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
_view.ListProduct.DataSource = bSource;
sda.Update(dbdataset);
}
}
private void btn_ok_Click(object sender, EventArgs e)
{
using (var con = SQLConnection.GetConnection())
{
var selects = new SqlCommand("Select * from employee_product where Codeitem =#Codeitem ", con);
selects.Parameters.Add("#Codeitem", SqlDbType.VarChar).Value = _view.txt_code.Text;
SqlDataReader reader;
reader = selects.ExecuteReader();
while (reader.Read())
{
data = (byte[])reader["Image"];
}
reader.Close();
var selectCommand = new SqlCommand("Insert into product_result (Image, Code, Name, Price, Discount, Quantity) Values (#Image, #Code, #Name, #Price, #Discount, #Quantity)", con);
selectCommand.Parameters.AddWithValue("#Image", data);
selectCommand.Parameters.AddWithValue("#Code", _view.txt_code.Text);
selectCommand.Parameters.AddWithValue("#Name", _view.txt_name.Text.Trim());
selectCommand.Parameters.AddWithValue("#Price", _view.txt_price.Text.Trim());
selectCommand.Parameters.AddWithValue("#Discount", txt_discount.Text.Trim());
selectCommand.Parameters.AddWithValue("#Quantity", txt_quantity.Text.Trim());
selectCommand.ExecuteNonQuery();
var select = new SqlCommand("Update employee_product set quantity = quantity - #Quantity where Codeitem= #Codeitem",con);
select.Parameters.AddWithValue("#Codeitem", _view.txt_code.Text);
select.Parameters.AddWithValue("#Quantity", txt_quantity.Text);
select.ExecuteNonQuery();
this.Close();
display();
}
}
You will see in this picture that image has a code. If user input another code it will get another image
You can save the image of the PictureBox to db like this....
MemoryStream getimagestream = new MemoryStream();
pictureBox1.Image.Save(getimagestream, _pb_photo.Image.RawFormat);
byte[] Imagedata = getimagestream.GetBuffer();
You can receive the image from db to picture box like this.
var ImagedataReceive = (byte[])reader["Image"];
var stream = new MemoryStream(ImagedataReceive);
pictureBox1.Image = Image.FromStream(stream);
You will need to provide a cell formatting method that puts the data into an image object the data gridview can use.
private void dgv_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (dgv.Columns[e.ColumnIndex].Name == "Image")
{
if (e.Value != null)
{
... convert byte[] to image...
}
}
}

how to load a default image from .ashx when SQL BLOB is unavailable

I am new to ASP & C# and have been unable to figure out how to do this.
I am loading BLOB's from a bd via an .ashx file like so <img src="getimage.ashx" /> and it works fine, but sometimes there is no BLOB or it is empty.
here is the basic code
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DataSource.iMIS.Connection"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT PICTURE_LOGO FROM Name_Picture WHERE ID = #EmpID", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("#EmpID", id);
con.Open();
byte[] pict = (byte[])cmd.ExecuteScalar();
con.Close();
ctx.Response.ContentType = "image/bmp";
ctx.Response.OutputStream.Write(pict, 0, pict.Length);
My thought is to check pict.Length right after con.Close() and if it fails, I want to display a default image, or even text.
Is that possible? How?
If you want to load an image from the disk when none is found in the DB, use this snippet.
public void ProcessRequest(HttpContext context)
{
//create a new byte array
byte[] pict = new byte[0];
//create a connection to the db and a command
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataSource.iMIS.Connection"].ConnectionString))
using (SqlCommand command = new SqlCommand("SELECT PICTURE_LOGO FROM Name_Picture WHERE ID = #EmpID", connection))
{
//set the proper command type
command.CommandType = CommandType.Text;
//replace the parameters
command.Parameters.Add("#EmpID", SqlDbType.Int).Value = id;
try
{
//open the db and execute the sql string
connection.Open();
pict = (byte[])command.ExecuteScalar();
}
catch (Exception ex)
{
//catch any errors like unable to open db or errors in command. view with ex.Message
}
}
//if no image found in database load the default from disk
if (pict == null || pict.Length == 0)
{
pict = File.ReadAllBytes(context.Server.MapPath("/noimage.bmp"));
}
//clear the buffer stream
context.Response.ClearHeaders();
context.Response.Clear();
context.Response.Buffer = true;
//set the correct ContentType
context.Response.ContentType = "image/bmp";
//set the filename for the image
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"ImageName.bmp\"");
//set the correct length of the string being send
context.Response.AddHeader("content-Length", pict.Length.ToString());
//send the byte array to the browser
context.Response.OutputStream.Write(pict, 0, pict.Length);
//cleanup
context.Response.Flush();
context.ApplicationInstance.CompleteRequest();
}
After a lot more searching and a lot of 'HttpCompileException(s)' I got this working.
Thanks to #Kazar for this answer here https://stackoverflow.com/a/2070493/3790921 and #Pranay Rana for this answer here https://stackoverflow.com/a/3801289/3790921
I put this together...
con.Open();
byte[] pict = (byte[])cmd.ExecuteScalar();
con.Close();
ctx.Response.ContentType = "image/bmp";
if (pict.Length <= 1) {
// the BLOB is not a picture
byte[] txt = ImageToByteArray(DrawText("no image found"));
ctx.Response.OutputStream.Write(txt, 0, txt.Length);
} else {
// stream the picture data from BLOB
ctx.Response.OutputStream.Write(pict, 0, pict.Length);
}
and it is working.

ASP.NET - How to display image in browser without saving image in temp?

I have a question- How to display image in browser without saving image in Temp folder? Is it even possible? My code have to reading images from database and displaying images in website. I actually try with converting data from database and I don't know what I want to do.
I try also with "imageHandlers", "FileStream", "Base64StringToBitmap" and nothing still works...
Please write example code or modify my code.
private void LoadImages()
{
ImageButton imageButton = new ImageButton();
string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Id, Name, Data from tblFiles WHERE email = #CurrentUser";
cmd.Parameters.Add("#CurrentUser", SqlDbType.NVarChar);
cmd.Parameters["#CurrentUser"].Value = User.Identity.Name;
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
sdr.Read();
string fileName = sdr["Name"].ToString();
FileInfo fi = new FileInfo(fileName);
byte[] byte_image_string = ((byte[])sdr["Data"]);
string image_string = Convert.ToBase64String((byte[])sdr["Data"]) + fi.Name;
imageButton.Height = Unit.Pixel(100);
imageButton.Style.Add("padding", "5px");
imageButton.Width = Unit.Pixel(100);
imageButton.Click += new ImageClickEventHandler(imageButton_Click);
Panel1.Controls.Add(imageButton);
System.Drawing.Image newImage;
if (byte_image_string != null)
{
using (MemoryStream stream = new MemoryStream(byte_image_string))
{
newImage = System.Drawing.Image.FromStream(stream);
//I want here display image to browser without saving
//string newPhoto = "";
//newImage.Save(newPhoto);
imageButton.ImageUrl = "data:image/jpg;base64," + newPhoto;
}
}
conn.Close();
}
}
}
}
}
My example image code from database:
0xFFD8FFE000104A46494600010100000100010000FFE1018C45786966000049492A0008000000020031010200070000002600000069870400010000002E00000000000000476F6F676C6500000500009007000400000030323230099007000B0000007000000086920700080100007B00000002A00400010000006F02000003A0
This is how I do it in one of my projects: Look src part
<p><a href='<%#"TheObject.aspx?O=" + Eval("ID") %>'><img runat="server" visible='<%#Eval("AttachmentID") != DBNull.Value %>' class="objPicture1" alt='<%Eval("Title") %>' width="340" height="260" src='<%#"~/Attachment.aspx?ID=" + Eval("AttachmentID")%>' /></a></p>
In Attachment.aspx you have this code:
protected void Page_Load(object sender, EventArgs e)
{
Guid objID = //take the ID of the object ?ID=""
DataRow attachmentRow = //fetch DataRow of the Attachment from Database
if (attachmentRow == null)
return;
Response.ContentType = attachmentRow["ContentType"].ToString();// value of this is image/gif or image/jpeg and etc.
Response.BinaryWrite((byte[])attachmentRow["Data"]); // Data is type image in my case
Response.End();
}
It looks like you are creating Base64String in wrong way. You are appending file name to it that should not work. just try following.
string image_string = Convert.ToBase64String((byte[])sdr["Data"]);
Assign directly it to ImageUrl. no need to use MemoryStream here.
imageButton.ImageUrl = "data:image/jpg;base64," + image_string;

asp.net display image(more than 2) from database using handler

Handler.ashx
public void ProcessRequest (HttpContext context)
{
string imageid = context.Request.QueryString["ImID"];
SqlConnection connection = new SqlConnection(con);
connection.Open();
SqlCommand command = new SqlCommand("SELECT PhotoStoreTB.Data FROM PhotoStoreTB INNER JOIN UserTB ON UserTB.UserID = PhotoStoreTB.UserID WHERE PhotoStoreTB.UserID ='" + imageid + "'", connection);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
byte[] imagedata = (byte[])dr[0];
context.Response.ContentType = "image";
using (System.IO.MemoryStream str = new System.IO.MemoryStream(imagedata, true))
{
str.Write(imagedata, 0, imagedata.Length);
Byte[] bytes = str.ToArray();
context.Response.BinaryWrite(bytes);
}
connection.Close();
context.Response.End();
}
Exception Thrown from context.Response.End();
{Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}
Aspx Code
<asp:Image ID="Image2" runat="server" ImageUrl='<%#"Handler.ashx?ImID="+ Eval("PUserID")%>'
Height="115px" Width="115px" CssClass="img-border"/>
I want to display multiple images in data list
Data List Bind
try
{
ld.Openconnection();
SqlCommand Cmd = new SqlCommand("PGetPropertyByCriteria", ld.con);
Cmd.Parameters.AddWithValue("#StartIndex", 1);
Cmd.Parameters.AddWithValue("#EndIndex", 10);
Cmd.Parameters.AddWithValue("#flag", "Get");
Cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter SqlAda = new SqlDataAdapter(Cmd);
DataSet DsStudentDetails = new DataSet();
SqlAda.Fill(DsStudentDetails);
if (DsStudentDetails.Tables.Count > 0 && DsStudentDetails.Tables[0].Rows.Count > 0)
{
TotalPage = (Int32.Parse(DsStudentDetails.Tables[0].Rows[0]["row"].ToString()) / PageSize) + ((Int32.Parse(DsStudentDetails.Tables[0].Rows[0]["row"].ToString()) % PageSize) > 0 ? 1 : 0);
CurrentRecord = DsStudentDetails.Tables[0].Rows.Count;
DataTable tDataTable = new DataTable("PagingTable");
tDataTable.Columns.Add(new DataColumn("LinkButtonVisible", typeof(bool)));
tDataTable.Columns.Add(new DataColumn("DisplayName", typeof(string)));
tDataTable.Columns.Add(new DataColumn("Value", typeof(string)));
tDataTable.Columns.Add(new DataColumn("LabelVisible", typeof(bool)));
dtlProduct.DataSource = DsStudentDetails.Tables[0];
dtlProduct.DataBind();
}
else
{
DLPAGING.DataSource = null;
DLPAGING.DataBind();
dtlProduct.DataSource = null;
dtlProduct.DataBind();
}
}
catch (Exception ex)
{
ex.ToString();
}
finally
{
ld.Closeconnection();
}
Please Help me to display multiple images to datalist from database
Try using Response.Clear(); at the begining of ProcessRequest function.
Or
you can use ApplicationInstance.CompleteRequest instead of Response.Clear();
Change your response content type from
context.Response.ContentType = "image";
To
context.Response.ContentType = "image/jpeg(png)"; depends on type
and remove context.Response.End()
Just see the below sample code
byte[] image = imagefromDB();
context.Response.OutputStream.Write(image, 0, image.Length);
context.Response.ContentType ="image-mime-type";
I can't assure Response.CLear(); is a good idea though. Sometimes it gets glitchy. I know, I've tried.
try using jpg with the image response instead of jpeg. Sometimes it doesn't work either. jpg works all the time.
When you use a Handler, each instance of where the handler is called has its own Response object. Therefore you can bind the dynamic paths of the images from the server code.
using a GridView and binding the RowDataBound event you can do this quite easily.
protected void gvAlbumImages_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow aRow = ((DataRowView)e.Row.DataItem).Row;
//This is the Row from your Datasource (which is a datatable)
Image img = (Image)e.Row[e.Row.RowIndex].FindControl("imgControl");
//get the Image object at the row you are binding to.
//now simply set the source from the value in the DataRow
img.ImageUrl = string.format("~/ImageHandler.ashx?ImageID={0}",aRow.Field<int>("ImageID")
}
}

Loading PictureBox Image From Database

i'm trying to load images from database to a PictureBox. I use these following codes in order to load them to my picture. I've written some code but don't know what I should do for continuing.
Any help will be appreciated.
private void button1_Click(object sender, EventArgs e)
{
sql = new SqlConnection(#"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True");
cmd = new SqlCommand();
cmd.Connection = sql;
cmd.CommandText = ("select Image from Entry where EntryID =#EntryID");
cmd.Parameters.AddWithValue("#EntryID", Convert.ToInt32(textBox1.Text));
}
Continue with something like this in the button1_Click:
// Your code first, here.
var da = new SqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds, "Images");
int count = ds.Tables["Images"].Rows.Count;
if (count > 0)
{
var data = (Byte[])ds.Tables["Images"].Rows[count - 1]["Image"];
var stream = new MemoryStream(data);
pictureBox1.Image = Image.FromStream(stream);
}
Assuming we have a simple database with a table called BLOBTest:
CREATE TABLE BLOBTest
(
BLOBID INT IDENTITY NOT NULL,
BLOBData IMAGE NOT NULL
)
We could retrieve the image to code in the following way:
try
{
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;
if(c>0)
{ //BLOB is read into Byte array, then used to construct MemoryStream,
//then passed to PictureBox.
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image= Image.FromStream(stmBLOBData);
}
cn.Close();
}
catch(Exception ex)
{MessageBox.Show(ex.Message);}
This code retrieves the rows from the BLOBTest table in the database into a DataSet, copies the most recently added image into a Byte array and then into a MemoryStream object, and then loads the MemoryStream into the Image property of the PictureBox control.
Full reference guide:
http://support.microsoft.com/kb/317701
private void btnShowImage_Click(object sender, EventArgs e)
{
string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\PIS(ACU).mdb;";
Con = new OleDbConnection(#constr);
Con.Open();
Com = new OleDbCommand();
Com.Connection = Con;
Com.CommandText = "SELECT Photo FROM PatientImages WHERE Patient_Id = " + val + " ";
OleDbDataReader reader = Com.ExecuteReader();
if (reader.Read())
{
byte[] picbyte = reader["Photo"] as byte[] ?? null;
if (picbyte != null)
{
MemoryStream mstream = new MemoryStream(picbyte);
pictureBoxForImage.Image = System.Drawing.Image.FromStream(mstream);
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(mstream);
}
}
HERE IS A TOTAL ANOTHER WAY TO DO SO:
You can simply convert the Image to Text before saving it into DataBase and the then convert it back to the Image after reading it:
public string ImageToStringFucntion(Image img)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] imgBytes = ms.ToArray();
string FinalText = Convert.ToBase64String(imgBytes, 0 , imgBytes.Length);
return FinalText;
}
}
catch
{
return null;
}
}
Now you can Insert or Update your Database...
Now Let's consider you want it back:
public Image StringToImage_(string input_)
{
try
{
byte[] imgBytes = Convert.FromBase64String(input_);
using (MemoryStream ms = new MemoryStream(imgBytes))
{
Image img = Image.FromStream(ms, true);
return img;
}
}
catch (Exception ex)
{
return null;
}
}
Now you can do as follow:
// Considering you have already pulled your data
// from database and set it in a DataSet called 'ds',
// and you picture is on the field number [1] of your DataRow
pictureBox1.Image = StringToImage_(ds.Table[0].Rows[0][1].ToString());

Categories

Resources