How to dispaly tiff images to image in ASP.net C#? - c#

Im using visual studio 2008 and i want to convert .tiff file and show it to img. I can display the image using the url get from website. But when im using the path url from server it say that the parameter is not valid. I search all in the internet but cant find a solution that could fix it. Hope you could help me. Thanks in advance.
Heres my code.
string filename = "";
file_name = "https://support.leadtools.com/SupportPortal/CS/forums/44475/PostAttachment.aspx"; (This is the link i get from website. It successfully display the image)
// but when im using this to get the tiff it says parameter is not valid. The path i show below is just an example
filename = "http://123.456.7.89:00/test/test.tiff";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(file_name);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
Bitmap bm = new Bitmap(s);

I used to get this same kind of issue in my application once, in my case it was caused due to larger file size which wasn't properly loaded in memory at the time of accessing it. Please check the size of file you are trying to display.Hope this
helps.

Related

Serve Image with IHttpHandler

I am working with a web server that is only available on our internal network. I have a need to make an image it serves available to the public via our DMZ web server. The internal system has a GUI where anyone can upload any type of image so my HttpHandler must be flexible.
The bit of code I am currently using is:
WebResponse webResponse = WebRequest.Create(imagePath).GetResponse();
using (Stream stream = webResponse.GetResponseStream())
{
context.Response.Clear();
context.Response.ContentType = webResponse.ContentType;
stream.CopyTo(context.Response.OutputStream);
context.Response.End();
}
This seems to work and it serves up images correctly on HTML pages, but when I right click in Chrome and select "Open image in new tab" I get garbled up text like this:
Any suggestions to make my code better or is this okay?
Thanks!
EDIT: Seems like this code snippet does exactly what it should do. The problem was with the internally hosted image itself. It Served the exact same garbled text even before running through this HttpHandler.
Your content type header is probably not being set correctly.
Change it to context.Response.ContentType = "image/png";, but replace "png" with whatever the image time you are loading happens to be.

.net Compact Framework 2.0 - OutOfMemory Exception When downloading an image

Im creating software for a Symbol MC75A using c# .net CF 2.0. We scan a barcode and it returns stock information but i am trying to add a feature that gets an image from a url. Each scan refreshes the screen with new data from database and also gets the image from the new url. It scans a few barcodes and returns maybe 4/5 images without issue then all of a sudden a OutOfMemoryException occurs. The code im using to Get Image is:
public Bitmap GetImage(string URL)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
myRequest.Method = "GET";
myRequest.AllowWriteStreamBuffering = false;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
myResponse.Close();
return bmp;
}
Called by:
pbImage.Image = GetImage(ProductImage);
pbImage.SizeMode = PictureBoxSizeMode.StretchImage;
I have tried to dispose of image before each new GET with pbImage.Image.Dispose() but still getting the same exception. The image sizes are between 100KB and 550KB. Whether it makes a difference the images sizes are always over 1000px each side.
Am i missing the correct way of disposing before re-getting or is it somehow caching all these images which then creates an outofMemory exception?
I have found my solution herer:
OutOfMemoryException loading big image to Bitmap object with the Compact Framework
It seems it is the system.drawing when decompressing the 2000px image it is creating an uncompressed image giving a larger size on the memory hense the exception. The solution shows a way to obtain a thumbnail of the image rather than the whole image.
Thanks for your help again.
Just incase someone has access to the image location. A better work around for me was to creat a webpage named resize.aspx and have the picture box populate from the page with the image filename as a parameter. Now that page (being on the server in the same location as the images) was able to resize the image and return a small 300x300px image. Seemed to be the best solution i could think of. This can also work for images at any url if you pass the full url of the image into another page and that page returns the required thumbnail.

Get Size of Image File before downloading from web

I am downloading image files from web using the following code in my Console Application.
WebClient client = new WebClient();
client.DownloadFile(string address_of_image_file,string filename);
The code is running absolutely fine.
I want to know if there is a way i can get the size of this image file before I download it.
PS- Actually I have written code to make a crawler which moves around the site downloading image files. So I doesn't know its size beforehand. All I have is the complete path of file which has been extracted from the source of webpage.
Here is a simple example you can try
if you have files of different extensions like .GIF, .JPG, etc
you can create a variable or wrap the code within a Switch Case Statement
System.Net.WebClient client = new System.Net.WebClient();
client.OpenRead("http://someURL.com/Images/MyImage.jpg");
Int64 bytes_total= Convert.ToInt64(client.ResponseHeaders["Content-Length"])
MessageBox.Show(bytes_total.ToString() + " Bytes");
If the web-service gives you a Content-Length HTTP header then it will be the image file size. However, if the web-service wants to "stream" data to you (using Chunk encoding), then you won't know until the whole file is downloaded.
You can use this code:
using System.Net;
public long GetFileSize(string url)
{
long result = 0;
WebRequest req = WebRequest.Create(url);
req.Method = "HEAD";
using (WebResponse resp = req.GetResponse())
{
if (long.TryParse(resp.Headers.Get("Content-Length"), out long contentLength))
{
result = contentLength;
}
}
return result;
}
You can use an HttpWebRequest to query the HEAD Method of the file and check the Content-Length in the response
You should look at this answer: C# Get http:/…/File Size where your question is fully explained. It's using HEAD HTTP request to retrieve the file size, but you can also read "Content-Length" header during GET request before reading response stream.

How to read picture from URL and show it on my page

I have a sql table which holds information:
id (hash)
imagename string
width int
height int
What is the best way to create .net image read which will show images in page. I would like to call it like image.aspx/ashx?id=[id] and function will try to catch and show that image.
I know how to get data from SQL but I dont know how to read img from URL and show it as image.
Could any please point me at some relevant information how to do it or show piece of code how it works?
Do I read it as stream?
Thanks
string imageFileName = "thefile.jpg";
context.Request.MapPath(#"IMAGES\" + context.Request.QueryString["id"]);
context.Response.ContentType = "image/jpeg";
context.Response.WriteFile(imageFileName);
context.Response.Flush();
context.Response.Close();
http://blogs.msdn.com/b/alikl/archive/2008/05/02/asp-net-performance-sin-serving-images-dynamically-or-another-reason-to-love-fiddler.aspx
http://msdn.microsoft.com/en-us/library/ms973917.aspx
Check out this article: http://aspnet-cookbook.info/O.Reilly-ASP.NET.Cookbook.Second.Edition/0596100647/aspnetckbk2-CHP-20-SECT-2.html
You'll want to create an HttpHandler class and wire that up in your web.config.
You can retrieve remote resources (such as images) via HTTP using the System.Net.WebRequest class.
WebRequest request = WebRequest.Create("http://www.doesnotexist.com/ghost.png");
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
BinaryReader reader = new BinaryReader(stream)
byte[] imageBytes = reader.ReadBytes(stream.Length);
Note that there might be better ways to read the bytes from the Stream. You should also remember to add using statements where appropriate to properly dispose of any unmanaged resources.

How to save a complete webpage using the built-in webbrowser in c#

Overall I am trying to write out a webpage to PDF. There is a web service that I can use to convert a file to pdf. So what I am trying to do is save out a webpage from the WebBrowser winforms control.
I have already tried writing it out the document stream but that just gives me the html of the page and not the images that are used with it.
Another way that I looked into, but have not been successful with, is trying to create an image of the WebBrowser document. I found some examples on the web that utilize the DrawToBitmap function but none of them have worked for me.
Any assistance would be grateful.
A long time ago I stumbled over this CodeProject-article 'Capture an HTML document as an image'
however, there is a new one (posted: 13 Feb 2010) 'HTML to Image in C#'
I haven't tested either of them but I think they should work!
You can take screenshots until you have the entire page using the Graphics.CopyFromScreen function.
// Get screen location of web browser
Rectangle rec = webBrowser1.RectangleToScreen(webBrowser1.ClientRectangle);
// create image to hold whats in view
Bitmap image = new Bitmap(rec.Width, rec.Height);
// get graphics to draw on image
Graphics g = Graphics.FromImage(image);
// Save into image
// From MSDN:
//public void CopyFromScreen(
// int sourceX,
// int sourceY,
// int destinationX,
// int destinationY,
// Size blockRegionSize
//)
g.CopyFromScreen(rec.X,rec.Y,0,0,rec.Size)
You may also want to remove the scrollbars so they aren't in your image:
webBrowser.ScrollBarsEnabled = false;
webBrowser.Document.Body.Style = "overflow:hidden;";
And then scroll down to take a shot of the next page:
webBrowser.Document.Window.ScrollTo(x,y);
To create the PDF, the program you're using will need the source code of the site. Wether you use the WebBrowser winforms control or something else to get that info, is of no real difference.
This code will get the source code of any site for you, presuming you don't need to upload stuff first:
string url = "some site";
string source = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using(StreamReader sr = new StreamReader(response.GetResponseStream()){
source = sr.ReadToEnd();
}

Categories