Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
Hi All,

Can i convert System.Web.UI.WebControls.Image to bytes so that i can save it to database.

Actually I am getting my image as a response of an aspx page (getimage.aspx), see below.
// send the image data down to the client
Response.Clear();
Response.AddHeader( "Content-Disposition", "attachment; filename=m-View_Image.jpg" );
Response.ContentType = "image/jpeg";
Response.TransmitFile( fileName );

// Writes the specified file directly to an HTTP response output stream
// without us having to buffer it in memory.
HttpContext.Current.ApplicationInstance.CompleteRequest();

I want to read that image and save it in database
I can display the image like below:
string url = "http://..../getimage.aspx"

Image img = new Image();
img.ImageUrl = url;
img.DataBind();


But how to convert the image coming as aspx response in to bytes ?

Please help me.

Thanks in Advance.
Posted
Updated 8-Jun-11 20:19pm
v3

1 solution

Everything is possible. However, I strongly recommend not to serialize UI controls.

UI can be a temporary item of a software project. If you serialize UI, you won't be able to upgrade it in a backup-compatible manner. It's very likely that later on you will need to migrate to a different UI library or a whole platform. You database, busyness logic and other application specific code will survive… in you refrain from creation of hard coupling of UI with data model.

What you need is loose coupling, see http://en.wikipedia.org/wiki/Loose_coupling[^].

Instead of serializing the UI, create a logical data model of you UI. It will be tightly bound to your functionality and not to UI elements. The model should be composed of pure data classes/structures. The relationship between data model and UI will be reduced to two operations: data to UI (population) and UI to data (acquisition). Yes, this is extra work, but it will pay very well of in near future.

Serialization of such model won't be a problem. I suggest you don't do it manually. My best advice is using Data Contract, see http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This method of serialization is the most non-intrusive. You don't have to modify any of your types to make them serializable with System.Runtime.Serialization.DataContractSerializer or System.Runtime.Serialization.Json.DataContractJsonSerializer; instead, you only add attribute to your types and members.

As your thinking about UI serialization shows pretty good confusion about application architecture, I would advice to familiarize yourself with the architectural patterns I listed in my past solution:
how to control Controlls of a user interface form through Functions (methods)[^].

Here is the explanation of architectural patterns in general: http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^].

You don't have to learn them, but you really need to get a good idea on how robust application architecture should be build.

—SA
 
Share this answer
 
v2
Comments
saxenaabhi6 9-Jun-11 2:05am    
Thanks for the links.. I will read them tonight.
My bad actually, I don't want to serialize UI I know its bad.
I just put this image control in my question to show how am i getting the image.

I should have rather asked : how to convert the incoming aspx response attached with an image to image bytes so that i can store them in database.
Sergey Alexandrovich Kryukov 9-Jun-11 10:10am    
What do you mean aspx response, what in it and how do you get it? All you have between client and server is only HTTP (which is some text)...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900