I store my image file into the database as binary type ?
if i want to get the image from the database and show it into aspx page.
I have two option
1.To build a handler
2.to use data uri scheme (which i prefer)
what option should i prefer to show the image
Handler is the way to go. Data URI would only be good if the images were very small, because the user will need to wait for a big page to download with images embedded rather than get the page super-quick then download images concurrently on different connections/threads.
Personally I prefer the HTTPHandler approach. It means that should you decide later on that you would prefer to store your binary images in some other fashion then you simply have to update the handler accordingly.
There's a good example of the HTTPHandler approach on this link;
http://www.worldofasp.net/tut/images/Displaying_images_in_ASPNET_using_HttpHandlers_92.aspx
Related
I have images stored in a SQL Server database with datatype image. I want to retrieve them and convert them to bitmap, and use them to create an asp.net web form image gallery for an online shopping web site.
Should I use <asp:Repeater> control, <asp:GridView> or data list control?
I don't want to use image path stored in the database
It's sad that the experts here are asking you questions as if you already know how to solve your problem which it is clear from your question you don't. Let me try and give you a little background and direction and I think you will be able to get closer to solving what you want to do.
Your images are just blobs in your sqlserver database which has no direct connection the web. The only way you can show images is you need to basically put them into an img tag with src= the location on your web server. What needs to happen is the location on the web server you choose must instead of reading a file from the file system of the server, must somehow grab the image from the database and then stream those image bytes to the img tag on the page.
THere are multiple ways to do that in asp.net. The easiest is a handler or ashx file (don't even know if those are supported anymore).
At anyrate, here is a link that might help. You might try googling something like "display image from sql server on asp.net" and see what else comes out. Obviously, lot's of people do this and you will to soon.
Good Luck.
https://www.aspsnippets.com/Articles/Display-image-from-database-in-Image-control-without-using-Generic-Handler-in-ASPNet.aspx
I have a .NET Server Control app that simply returns some HTML. I also need to embed several picture files into the assembly so that the HTML file can use them as its src= for each of them.
We will simply have a .HTML file that lives in the project as an embedded resource and the server control code will read this html and serve it up. Within THAT html, we will need to have all the picture src links (as well as CSS, js, etc) to point back to embedded resource files.
Does anyone know what code I would put in the HTML file for the pictures to make it point back to the embedded picture file?
I have to do this on a grand scale... hundreds of times. I really would like a programmatic approach to doing this so I can write a wrapper and never have to touch it again when we update the server control with new html, picture files, etc.
One might imagine a way to do this at compile time where I can loop through the embedded files with GetManifestResourceNames and then replace() the src links with the HTTP resource links I suppose?
Thank you for any guidance!
Hm, your question covers quite many aspects. Let me repeat to see if I got it: You have an assembly, with a raw HTML file in it. This file references some items, which are to be found within this same assembly, and you want to have them served to the client upon request as well.
One possible solution might be this.
Instead of a raw HTML file, use a templated one. Then, feed all available resource names as proper URL's into the templating engine, to replace the placeholders.You may want to look at DotLiquid for this.
Create a HTTP handler for each file type you want to serve. Inside the handler, you pull the item from the resources of the dll and serve them.
Alternatively, if those resources are rather small, you want to have a look into the data URI scheme, to save the extra requests and omit the handler. With this you could replace the placeholders with the data URI's directly, and serve a single HTML file with everything in it in the frist place.
Another choice is to have your .NET Server Control app check for optional GET arguments and return the image instead of the HTML.
Your original HTML request might be a simple:
GET netServerApp
Which returns the HTML with normal embedded links.
The HTML image links in the HTML might look like this:
<img src="netServerApp?src=Image1.svg">
or the like. Your server app would then return the appropriate image, instead of the HTML.
It means several round trips to get everything, but that is normal for HTML anyway.
I would like to know a most efficient way to load external images to my website.
For example:
My website's url is "www.mydomain.com". The external image is http://www.myimagedomain.com/image.jpg.
The most common way is to write a simple html image-tag like
<img src="http://www.myimagedomain.com/image.jpg" />.
The problem is, if the requested image is very large (8000x6000 pixel) but I want to show this picture as a thumbnail/preview like 200x200 pixel, e.g on mobile devices.
Based on this information I wrote a little ashx (c#) handler that downloads the requested image and resizes it to a given weight/height parameters, like this:
<img src="http://www.mydomain.com/img.ashx?imageUrl=http://www.myimagedomain.com/image.jpg&w=200&h=200" />
Now there is another problem, because the httphandler always downloads the requested image, on-the-fly.
My new approach is to generate a based64 string from the resized image and save this in a database once?!
Would you recommend this or is there another way to eliminated the download problem?
Maybe someone know how google-image-search prevents this problem?
I don't want to save the external images on my own server...
I would suggest to use image resizer library, it solves much of what you need in efficient way - caching included:
http://www.nuget.org/packages/ImageResizer/
I think google caches image thumbnails on its servers for search.
This one will be interesting...
I have seen many asp.net thumbnail generation tutorials / sample code, but no one has considered the problem of concurrency access when generating thumbnail image dynamically, when one or more user access the same page when the thumbnail needs to be generated.
A simple case, i have a site with property images (houses etc.), images are stored in a folder, the thumbnails are generated (for gallery) when someone first time accesses particular offer, then a handler makes the thumbnails from original larger images, the handler generate each thumbnail only once and then use the generated image in further requests.
What happens if two users access this page in the same time, the handler could run twice on the same file or more, there could be concurrency problem, file opening errors and so on (file needs to be opened for thumbnail generation).
Normally one user gets the thumbnail and other get a blank box without image till they refresh the page (since the first user triggered the thumbnail creation)
So the question is, how to avoid this situations ?
Normally if you are only opening the original image file for reading in order to generate the thumbnail there is no problem accesing it concurrently. Multiple users can open the same file for reading at the same time. Problems arise if you start writing at the same time.
I have an image gallery that is created using a repeater control. The repeater gets bound inside my code behind file to a table that contains various image paths.
The images in my repeater are populated like this
<img src='<%# Eval("PicturePath")' %>' height='200px' width='150px'/>
(or something along those lines, I don't recall the exact syntax)
The problem is sometimes the images themselves are massive so the load times are a little ridiculous. And populating a 150x200px image definitely should not require a 3MB file.
Is there a way I can not only change the image dimensions, but shrink the file size down as well?
Thanks!
I would recommend creating a handler that can resize images for you on the fly and encode them in whatever format you like.. kind of like a thumbnail generator. This will cost CPU on the server but you can cache images and severely reduce bandwidth costs ETC. Let me see if I can find the link to a good article I read on something similar.
You can look at this article it isn't the one I had read but it has some info about how you can go about implementing this.
You're looking for the GetThumbnailImage method of the Image class. You will either want to generate the thumbnail images ahead of time or create the image the first time it is accessed and save it to disk for later use (so first access would be slow but subsequent requests would be quick).
You could try either of these 2 projects on CodePlex.com, both offer dynamic image generation with caching.
Dynamic Image Process
ASP.NET Image Generation
The later is straight from Microsoft.