Save image from System.Web.UI.WebControls.Image to database

Save image from System.Web.UI.WebControls.Image to database

I do not know the coding on:
1. How to save image from System.Web.UI.WebControls.Image control to the database.
2. The code the retrieve the image from the database and display back onto the control.

The project that I am doing is in WEBFORM. The project reads a smartcard of the user and display the data inside the smartcard onto control such as textboxes and image control. Then save it into the database.

I read the image by:- (with success)
mykad.SaveImage(Server.MapPath("MyKad.bmp"));                    
imgPicFace.ImageUrl = "~/MyKad.bmp";

The problem is now I do not know how to SAVE and RETRIEVE.

CODING ATTEMPT FOR SAVE (worked in winform, but not in webform)

 
1    SqlDataAdapter DataAdapter = new SqlDataAdapter(commandString, connectionString);
2    DataSet dataSet = new DataSet();
3    DataAdapter.Fill(dataSet);
4    DataTable dataTable = dataSet.Tables[0];
5    DataRow newRow = dataTable.NewRow();
6    
7    newRow["IC_No"] = txtICNo.Text;
8    newRow["Name"] = txtName.Text;
9    newRow["Address1"] = txtAddress1.Text;
10   newRow["Postcode"] = txtPostcode.Text;
11   newRow["City"] = txtCity.Text;
12   newRow["State"] = txtState.Text;
13                   
14   if (imgPicFace.ImageUrl != null)
15   {
16   	MemoryStream stream = new MemoryStream();
17   	imgPicFace.Image.Save(stream, ImageFormat.Jpeg);
18   	stream.Position = 0;
19   	byte[] data = new byte[stream.Length];
20   	stream.Read(data, 0, Convert.ToInt32(stream.Length));
21   	newRow["Photo"] = data;
22   }

 
CODING ATTEMPT FOR RETRIEVE (worked in winform, but not in webform)

 

1    SqlDataAdapter DataAdapter = new SqlDataAdapter(commandString, connectionString);
2    DataSet dataSet = new DataSet();
3    DataAdapter.Fill(dataSet);
4    DataTable dataTable = dataSet.Tables[0];
5    
6    byte[] rightFP;
7    
8    int auth = -1;
9    
10   foreach (DataRow dr in dataTable.Rows)
11   {
12                   if (!(dr["Right_FP"] is System.DBNull))
13                   {
14                       rightFP = (byte[])dr["Right_FP"];
15                       auth = mykad.Authentify(rightFP, mykad.RightFinger_Byte);
16   
17                       if (auth == 0)
18                       {
19                           Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", common.mypopup("Match Found!"));
20                           lblMatch.Text = "Matching: Success";
21   
22                           #region Alpha-numeric
23                           txtICNo.Text = dr["IC_No"].ToString();
24                           txtName.Text = dr["Name"].ToString();
25                           txtAddress1.Text = dr["Address1"].ToString();
26                           txtPostcode.Text = dr["Postcode"].ToString();
27                           txtCity.Text = dr["City"].ToString();
28                           txtState.Text = dr["State"].ToString();
29                           #endregion
30   
31                           #region Picture
32                           if (!(dr["Photo"] is System.DBNull))			//Getting the Bitmap from the DB
33                           {
34                               byte[] photoByte = null;
35                               int ArraySize;
36                               photoByte = (byte[])dr["Photo"];
37                               ArraySize = photoByte.GetUpperBound(0);
38                               System.IO.MemoryStream ms = new System.IO.MemoryStream(ArraySize + 1);
39                               ms.Write(photoByte, 0, ArraySize + 1);
40                               imgPicFace.ImageUrl = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(ms);
41                               ms.Close();
42                           }
43   
44                   }
45   }
46   
47   
  


Please help with detail coding that works.

0
premierax5
3/6/2008 6:24:11 AM
asp.net.web-forms 93655 articles. 6 followers. Follow

1 Replies
1754 Views

Similar Articles

[PageSpeed] 30

premierax5:
1. How to save image from System.Web.UI.WebControls.Image control to the database.
2. The code the retrieve the image from the database and display back onto the control.
 

Hi premierax5,

Base on my experience, the following article is what you want! 

http://www.beansoftware.com/ASP.NET-Tutorials/Images-Database.aspx

It's written by VB and you can use the following to convert VB to C# if you are not familiar with VB: http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx

Hope it helps,

Hong Gang

 


Hong-Gang Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Hong
3/10/2008 11:14:10 AM
Reply:

Similar Artilces:

Save image from System.Web.UI.WebControls.Image into database
Hi! The objective of this post is to know:- How do I save an image displayed using System.Web.UI.WebControls.Image control into the database. How do I retrieve the image from database and display it again onto the System.Web.UI.WebControls.Image control. The image will be saved into the table as datatype: imageThe project that I am doing:-A webform (web-application) project.I read a smartcard that contains information of a user. The smartcard contains the photo of the user, name, address, etc.The image retrieved from the smartcard is saved into the current project folder. Using Server...

System.Drawing.Image TO System.Web.UI.WebControls.Image
Is there a way to convert an System.Drawing.Image to an System.Web.UI.WebControls.Image? Im getting the image from an XML file, the binary being in base 64 string. I then convert it back to an array of bytes. My problem is that I can't find a way to create the System.Web.UI.WebControls.Image with a Stream or an array of bytes... I got the Image from Drawing but I can't convert it back to a WebControl Image. I suppose I must write down the file somewhere on the server then return the path but I dont want to leave these temporary images on the drive. Any ideas or suggestion anyone?...

System.Drawing.Image to System.Web.UI.Webcontrols.Image
How do I convert this?DaveHelixpoint LLCHarrisburg Web Developer These two are entirely two different classes meant for different things: System.Drawing.Image represents the binaray/raw form of an image where as System.Web.UI.WebControls.Image is a ASP.NET server-side control used to render an image.Mark replies as answers if they helped you solve the problem.  You should save the image to a physical file in your web application folder and set the ImageUrl property of the Image control to the virtual path. example belowSave image to a physical folder.Once it is saved (suppose you sa...

'Image' is an ambiguous reference between 'System.Web.UI.WebControls.Image' and 'System.Drawing.Image'
In my aspx page, I have the following snippet: <%@ Page Language="C#" %><%@ Import Namespace="System.IO" %><%@ Import Namespace="System.Drawing" %>     public System.Drawing.Image Test()    {      ...    }  I keep getting the 'Image' is an ambiguous reference between 'System.Web.UI.WebControls.Image' and 'System.Drawing.Image' error in my Test function (which returns an Image). How do I fix this? Right which is possible; use the full...

CS0104: 'Image' is an ambiguous reference between 'System.Web.UI.WebControls.Image' and 'System.Drawing.Image'
 CS0104: 'Image' is an ambiguous reference between 'System.Web.UI.WebControls.Image' and 'System.Drawing.Image'   <%@ Page Language="C#" %> <%@ Import Namespace="System.Drawing" %> <%@Import Namespace="System.Drawing.Imaging" %>   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script type="text/C#" runat="server"> void Page_Load() {//The @ means "Ignore escape characters!" in the following string. It is a verbatim //literal string - any backslashes (and newlines) in...

convert System.Web.UI.WebControls.Image into System.Drawing.Image
Hi!is there some way using that I can typecast System.Web.UI.WebControls.Image into System.Drawing.Image? I want to convert an image from System.Web.UI.WebControls.Image to System.Drawing.Image. Thanks in advance... VB        Dim img As System.Drawing.Image        img = System.Drawing.Image.FromFile(Server.MapPath(Image1.ImageUrl)) C#        System.Drawing.Image img = null;        img = System.Drawing.Image.FromFile(Server.MapPath(Image1.ImageUrl));  ...

Convert System.Drawing.Image into System.Web.UI.Webcontrols.Image
hi, whenever i retrive image from the database as  bytes array, i can convert bytes array into bitmap but i cannot assign bitmap(System.Drawing.Image)  to image control(System.Web.UI.Webcontrols.Image). it is possibe to convert this. please provide me a solution. by venkatesan T. Hi,       One way is save the Image(System.Drawing.Image) in a path ,and give the saved path to ImageUrl of the ImageButton .For Eg: System.Drawing.Image img = System.Drawing.Image.FromFile(@"c:/inetpub/wwwroot/Forum/Undo.gif"); string filename="c:/inetpub/wwwroo...

Show Image on the System.Web.UI.WebControls.Image
Hi I have image stored as bytes in the SQL Database. I can read this into byte array depending on the user selection. Now I want to show the loaded image (which is in byte array) in the image control on my aspx page. Can somebody help me with this You'll want to set the ImageUrl tag to an ASPX page, and then make that ASPX page retrieve the image and output it to the http output stream. See this article for more info: http://www.aspfree.com/c/a/ASP.NET/Retrieving-Images-from-a-Database--C---Part-II/--Rob WallingASP.NET MVPMy blog, Software by RobPlease click "Mark as Answer&...

Converting 'System.Drawing.Image' to 'System.Web.UI.WebControls.Image'
Hello,I have an image stored on disk, that I am manipulating using the Drawing classes, and creating a 300x400 thumbnail from. Then, I want to display this image in an Image control on my webpage. How do I do that?Code:                    //Create the delegate                    System.Drawing.Image.GetThumbnailImageAbort dummyCallBack;           &nbs...

Help to retrieve image from database and display in Web.UI.WebControls.Image
Hi! I am a newbie. I want to retrieve image from database and display it in Web.UI.WebControls.Image.The control name is imgPicFace.And I want when it retrieve from database the control point like this >>>  imgPicFace.ImageUrl = "~/MyKad.bmp"; Please help and provide example code for me. Thank you.SAVE CODE:- 1 Bitmap b = new Bitmap(Server.MapPath("MyKad.bmp")); 2 System.Drawing.Image i = (System.Drawing.Image)b; 3 4 MemoryStream streamP = new MemoryStream(); 5 ...

Dynamicly created System.Web.UI.WebControls is not copied to staticly creadted System.Web.UI.WebControls
Below is dynamicly created Table  System.Web.UI.WebControls.Table Tablex = new Table(); Below is staticly created Table  <asp:Table ID="Table1" runat="server"></asp:Table>   After some operations over Tablex, I want to make Table1 = Tablex. But Table1 did not take effect. I'm sure Tablex contains rows and cells that has data inside them. Table1 = Tablex; //No take effect   Thanks Why can't you just make the changes to Table1.  What is the need for creating a dynamic table and then assigning it to static table. ...

difference between "using System.Web.UI.WebControls" or "System.Web.UI.WebControls.ServerValidateEventArgs"
Hi all, quick question. I'm writing a custom validation method. In the declaration it has: protected void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args) Is there any difference between doing that, vs doing: using System.Web.UI.WebControls; protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) Do they do the same thing? Is it any more efficient when processing the request to do it one way or another? Is there a technical term for what i am talking about? Thanks in advance for any explanation ...

System.Drawing.Bitmap vs. System.Web.UI.WebControls.Image
Hi,I am getting a problem with assign a System.Drawing.Bitmap to a System.Web.UI.WebControls.Image  First I want to show my code to draw a graphics in which g is: g = Graphics.FromImage(DrawingBitmap);1 protected void DrawGraphic(object sender, EventArgs e) 2 { 3 g.Clear(Color.WhiteSmoke); 4 5 draw24Hours(g); 6 7 DrawingBitmap.Save(Server.MapPath("sample.jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg); 8 WebControlImage.ImageUrl = Server.MapPath("sample.jpeg"); 9 ...

Upcast System.Web.UI.Control to System.Web.UI.WebControl?
I'm getting a "Specified cast is invalid" error when trying the following: WebControl wc = (WebControl)control; Where "control" is a System.Web.UI.Control inherited from a ControlsCollection (Panel.Controls). Is it possible to upcast from Control to WebControl? Seems silly not to considering the Control is a TextBox or CheckBox which inherits from WebControl. If this isn't possible... how can I access the "Attributes" of a Control? If it were a WebControl, I'd do the following: wc.Attributes["onclick"] = "...

Web resources about - Save image from System.Web.UI.WebControls.Image to database - asp.net.web-forms

Request a demo - Connexus Support
Business Internet ASL SYMMETRIC ETHERNET HYBRID Hosting DOMAIN + WEB CO-LOCATION FACILITIES MAIL Networks MPLS / VPN SSL / VPN Web Tool WEBCONTROL ...

SEO Blog - Search Engine Optimisation / Search Engine Marketing & general IT blog
Read the AKA Marketing search engine optimisation blog for posts about the latest organic search engine optimisation and search engine marketing ...

ALSEDI Group. Excellent Software. Excellent Usability.
ALSEDI Group Portal. High quality Surveillance, Privacy Protection and Desktop Enhancement software.

Case insensitive compare on string in ASP.NET - Coders Grid
using System ; using System.Collections.Generic ; using System.Linq ; using System.Web ; using System.Web.UI ; using System.Web.UI.WebControls ...

advsearch
Hong Kong 繁体中文 / English China 简体中文 United States English Canada Francais / English Central-South America & Caribbean Region English / Espanol ...

Copyright Information - Media Temple
The (mt) Media Temple (mediatemple.net) web site, including all text, HTML, scripts, and images are copyright 1998-2014. All rights reserved. ...

New Microsoft DevRadio Episode: World of Warcraft API Starter Kit for Windows Phone
This is the blog of Dave Isbitski. A Technology Evangelist for Microsoft based out of Philadelphia, PA USA. He is currently focused on helping ...

Adventures in Worstpracticeville
This morning, I wrote ~4500 lines of code in about two minutes. Skeptical? I thought you would be, so here's the curious tale of how I deviated ...

ConditionalRequiredTextValidator Control
The ConditionalRequiredTextValidator control allows you to specify a CheckBox, RadioButton, CheckBoxList, RadioButtonList, DropDownList, or ListBox ...

IT Managed Service Providers with Datacenters in India - Netmagic
As a leading Managed service provider with datacenters in India, Netmagic solutions, fulfills your entire IT infrastructure requirements: from ...

Resources last updated: 11/30/2015 8:22:49 AM