How to use Image conrtrol in asp.net (C# web) - c#

I have an image stored in my MS Access database with the data type OLE Object. I want it to display in an Image control. How can I do this? I tried this, but only in a PictureBox control in windows forms. Please help. Thanks in advance.

You need to create a page that can read the image from the database as binary data, then write that data directly to http response as binary data using Response.BinaryWrite to feed the image out. Then the src attribute of your image is pointed at the page itself like:
<img src="image.aspx" />
And the code behind image.aspx:
// An assumed method to get binary data our of the database
var bytes = YourDataLayer.GetBinaryImageData();
Response.Clear();
Response.AddHeader("Content-Disposition","attachment;filename=filename.jpg");
Response.ContentType = #"image\jpg";
Response.BinaryWrite(bytes);
Response.End();

read the image from the database as byte array and then create a temperary image object and assign it to the webcontrol on the page

Related

How to open image in new tab from generic handler?

I managed to display the binary image from database to image control using generic handler. I want to open the image on new tab or copy it's link address using right click from the mouse. But the URL used is the address of the generic handler
(http://localhost:1948/admin/imghndlr.ashx?serial=qwertyuiop). I tried searching but didn't get any results, maybe wrong keywords used. Below is the screenshot of the image in new tab.
How are you displaying the image?
Your View File should look something like this, of course you need to change data:image/jpeg on what file extension your using.
<img alt="" src="data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAsMAAAGhCAIAAAALOi7ZAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3QgLEhM6PUSGrwAAIABJREFUeNq8vcuSLEmWHKZ6jnlEZt5761Z3T/eAHAICAYRcEALsuOCWPzbzDfwP/gKXWJACoRDCBSkEBgPhADKY7qnu+4wIdztHuThmHh55q2t6ho+SlpaqyMwID3ez89CjqsY//dM//bM/+zMc/pGE3//PT/z09/1I0t/1Rz/x+o9+0I++vv/n8fU/8MW/9U9+9JVvL/v/u1cy86cv5ttfePXKq//8fTfhp+/qT3/oq8v+6V/+Ay/v25/+4X/46nqO"/>
Also if its a byte file dont forget to Convert it.
Convert.ToBase64String(Foto);
You need add data mime-type to make browser know what type does you responsed data is. added code look like below:
public void ProcessRequest(HttpContext context)
{
//image/png is png mime
context.Response.ContentType = "image/png";
//read buffer from database
context.Response.BinaryWrite(buffer);
}

Insert image into asp.net image tag

I'm trying to display my picture out on the image tag at my webform in a web application which i used VS2012 to develop. I have already stored my image in a binary form in the sql server 2008. I attempted to display out the image through the c# codes. However, the image displayed out is the exact dimension size. I'm trying to store this particular selected into the image tag with a specific dimension. Here are my codes which i used to display out my image.
This code here is for me to select out the picture from the database. I managed to display the picture out by using this.
string strQuery = "select profilepic from LoginRegisterOthers where username=#username";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("#username", SqlDbType.VarChar).Value = username;
DataTable dt = GetData(cmd);
if (dt != null)
{
download(dt);
}
This code then used to convert the image binary data into an image.
private void download(DataTable dt)
{
Byte[] bytes = (Byte[])dt.Rows[0]["profilepic"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpg";
//Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["Name"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
What you do with the binary array needs to change (if I guessed correctly what you're trying to do)
This is an alternative technique of inserting binary contents into image tag.
Instead of just streaming the binary contents down to the browser, you could write it in the src attribute of the <img> tag. The HTML you are aiming to have would look like this example (note that the image contents is Base64-encoded):
<img src="data:image/gif;base64,R0lGODlhUAAPAKIAAAsLav///88PD9WqsYmApmZmZtZfYmdakyH5BAQUAP8ALAAAAABQAA8AAAPb
WLrc/jDKSVe4OOvNu/9gqARDSRBHegyGMahqO4R0bQcjIQ8E4BMCQc930JluyGRmdAAcdiigMLVr
ApTYWy5FKM1IQe+Mp+L4rphz+qIOBAUYeCY4p2tGrJZeH9y79mZsawFoaIRxF3JyiYxuHiMGb5KT
kpFvZj4ZbYeCiXaOiKBwnxh4fnt9e3ktgZyHhrChinONs3cFAShFF2JhvCZlG5uchYNun5eedRxM
AF15XEFRXgZWWdciuM8GCmdSQ84lLQfY5R14wDB5Lyon4ubwS7jx9NcV9/j5+g4JADs=
" alt="British Blog Directory" width="80" height="15" />
Now, to accomplish this, you have probably several options. It all depends how you're rendering the <img> tag. If you're using a server-side control, then code like this may do (this sample snippet comes from this SO answer):
byte[] picByteArray = user.Picture.ToArray();
string myPicString = Convert.ToBase64String(picByteArray);
myPicture.Attributes["src"] = "data:image/gif;base64," + myPicString;
ADDITION:
One possible benefit of going that way is that, if design allows it or requires it, you could retrieve more than one image in a single database trip.
But as I also say in the comments, this method comes with the downside that images are processed synchronously, as your page is processed.
Have a look at this url. One of the users has created a function that takes a System.Drawing.Image as input. You will probably have to write the binary data you download from your db into an object of type System.Drawing.Image before you call the function that is written in the page below
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a43fe625-a5cd-4438-895f-3ddeec6eb866/
You have two choices:
in tag specify width and height of the image using CSS or html attributes. This causes that users will always download full size image but it will be displayed cropped
in src of img tag put width and height like: image.aspx?width=200&height=100. Parse query string on server side and resize image on server.
Of course you can use mix of both.

how to grab an image out of a DIV with C#

is it possible to grab an image that is embedded in a div via C# and attach it to an email?
I've got the code for attaching and sending images via email, I just don't know how to get it to grab the specific item out of the div and email it.
The image varies with a previous user selection so I can't just apply a static address of an image.
any ideas?
You need to grab image from inbound email?
Read more about MIME format: all images in email message are encriped in base64 format in MIME message. To grab your image you need to parse your emailmessage, read base64 string, wich encript your image, and decoding this string like this:
var byties = Convert.FromBase64String(Body)
I would look at using the microsoft html control, read the document and use the DOM to get the url of the image, download it using the http and then attach it to your email
for reference
How to download the image
Reading the Dom from visual studio
Use HtmlAgilityPack and HttpWebRequest.

Loading image based on URL

In .NET(C#), I'm loading an image. The src of the image is stored in the database.
I currently retrieve text from my db using this:
TextBox4.Text = reader["descr"].ToString(); // snippet
However, I want to know, how would I display an image?
Image1.Text= reader["img1"].ToString();
and then in my WebForm:
<asp:Image ID="Image1" runat="server" />
Use the ImageUrl property instead.
Try
Image1.ImageUrl = reader["img1"].ToString();
Have you tried Image1.ImageUrl = reader["img1"].ToString();
This one has been answered many times on so:
Basically you need to create an image handler to load the binary stream of the image to the browser. Also if you are going this route, please remember to use the cache header for each image so you are not pulling data from your database on every request.
ASP.NET [Image Handler]
Enjoy!

How to add System.Drawing.Image object to Asp:Image Control

How to add System.Drawing.Image object to Asp:Image Control but I dint have imageId in my table.Image field is in another table..i retrieve image from database in Image object.i want to display it in asp:Image control or grid view.how to do it?? pls send reply... C#
The Image class and <asp:image> control are completely different thing.
You need to have a page or http handler to write the Image object as an image file such as .jpg / .png / .gif. And then use the <asp:image> control to access the URL of your page or http handler to get the image file you generated and display it. <asp:image> basically only render a HTML <img> tag with the URL of the image.
You can use this ASP.NET module and the SqlReader plugin to serve images directly from the database.
You can then reference the images like so:
<asp:Image runat="server" ImageUrl="/sqlimages/[imageidhere]" />

Categories