Click to See Complete Forum and Search --> : [RESOLVED] save Image in database or on webserver


dannystommen
March 19th, 2009, 11:19 AM
Hello,

My problem is the next: I have a winform app and a webapp using both the same database. What I now do is the next: using the winform app I save a System.Drawing.Image in the database. I convert the Image to byte[] and the otherway around when I select the image from the database. I show this image in a picturebox.

But the problem is, I cannot show this image in the webapp, which is type of System.Web.UI.WebControls.Image.

I googled to convert System.Drawing.Image to System.Web.UI.WebControls.Image, but for as far I could find, this is not possible.

So I decided to upload the images to the webserver. In the webapp I can not easily say ImageUrl="myimage.bmp".

The problem now is the winform app. I am able to download the image from the server, using the WebClient, but I can't get the uploading part working.


System.Net.WebClient client= new System.Net.WebClient();
client.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
System.Drawing.Image img = System.Drawing.Image.FromFile(@"C:\temp\blue.png");
byte[] data = new byte[0];
using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
data = ms.ToArray();
}

byte[] result1 = client.UploadData("http://mydomain.com/Images/blue.png", data);
byte[] result2 = client.UploadFile("http://mydomain.com/Images/blue.png", @"C:\temp\blue.png");


Both UploadData and UploadFile gives WebException: "The remote server returned an error: (405) Method Not Allowed."

nelo
March 19th, 2009, 12:45 PM
Hello,

I think you should go back to storing the image bytes in the database. Use an HttpHandler to render the content for the images dynamically. In the http handler code you will be able to add the images bytes to response and specify the type of content. There are loads of samples of varying degrees of complexity on the web. Here's a good place to start.

http://msdn.microsoft.com/en-us/library/ms972953.aspx

If you look around you might find examples that go straight to the point.

dannystommen
March 20th, 2009, 05:04 AM
Nelo, thanx for you advice. I looked through the example, and I tried it. It displays the image in the browser, but all the layout is messed up by saving the System.Drawing.Image to the Response.OutputStream.

I think I will go for uploading the image to the webserver instead off saving in the database. I have the uploading part working. I didn't know a asp.net page should handle the upload instead of trying to upload the image straight to the webserver like I did in the first post.

The working code:

the winform app code:

System.Net.WebClient client= new System.Net.WebClient();
client.Credentials = new System.Net.NetworkCredential("username", "password", "domain");

byte[] result = client.UploadFile("http://mydomain.com/Upload.aspx", @"C:\temp\blue.png");


the upload.aspx page_load code

protected void Page_Load(object sender, EventArgs e) {
for (int i = 0; i < Request.Files.Count; i++) {
Request.Files[i].SaveAs(Server.MapPath("Images\\") + Request.Files[i].FileName);
}
}

nelo
March 20th, 2009, 06:30 AM
...I looked through the example, and I tried it. It displays the image in the browser, but all the layout is messed up by saving the System.Drawing.Image to the Response.OutputStream.

I think I will go for uploading the image to the webserver instead off saving in the database...
In what way was the layout messed up? I've used http handlers with success before (although I was using them with the html image not and not the asp.net server side equivalent). Anyway if the image is not data driven or if the data doesn't change frequently you could argue that it is better to store it on disk. :)

dannystommen
March 20th, 2009, 07:01 AM
The situation I am talking about in this case to save a site (url) with some details and a logo. The logo (almost) will never change, and only the 'top 100' sites will get a logo.

I think it's best to upload. It saves a column in the database (only 100 sites will have a logo, out of 5000 in total).