How to open image in new tab from generic handler? - c#

I managed to display the binary image from database to image control using generic handler. I want to open the image on new tab or copy it's link address using right click from the mouse. But the URL used is the address of the generic handler
(http://localhost:1948/admin/imghndlr.ashx?serial=qwertyuiop). I tried searching but didn't get any results, maybe wrong keywords used. Below is the screenshot of the image in new tab.

How are you displaying the image?
Your View File should look something like this, of course you need to change data:image/jpeg on what file extension your using.
<img alt="" src="data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAsMAAAGhCAIAAAALOi7ZAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3QgLEhM6PUSGrwAAIABJREFUeNq8vcuSLEmWHKZ6jnlEZt5761Z3T/eAHAICAYRcEALsuOCWPzbzDfwP/gKXWJACoRDCBSkEBgPhADKY7qnu+4wIdztHuThmHh55q2t6ho+SlpaqyMwID3ez89CjqsY//dM//bM/+zMc/pGE3//PT/z09/1I0t/1Rz/x+o9+0I++vv/n8fU/8MW/9U9+9JVvL/v/u1cy86cv5ttfePXKq//8fTfhp+/qT3/oq8v+6V/+Ay/v25/+4X/46nqO"/>
Also if its a byte file dont forget to Convert it.
Convert.ToBase64String(Foto);

You need add data mime-type to make browser know what type does you responsed data is. added code look like below:
public void ProcessRequest(HttpContext context)
{
//image/png is png mime
context.Response.ContentType = "image/png";
//read buffer from database
context.Response.BinaryWrite(buffer);
}

Related

itextsharp hyperlink a image and have it open in default viewer when picked

I have a pdf file created with itextsharp with images in the file. I would like to put a hyperlink in the file that if you pick the picture it will open that picture in a picture viewer. I can set a hyperlink to a web address but have no idea how to get it to open a file. Below is the code, yes I know that c:\test.jpg is a bad hardcoded file name but it is just a test. When you click the picture it does nothing but I have no idea how to tell it what to do.
iTextSharp.text.Image pic =TextSharp.text.Image.GetInstance(comment.examplePic);
pic.ScaleToFit(200f, 200f);
Chunk cImage = new Chunk(pic, 0, 0, false);
Anchor anchor = new Anchor(cImage);
anchor.Reference = "c:\\test.jpg";
doc.Add(pic);
doc.Add(anchor);
A PDF is self-contained. This means that all the resources needed to show the PDF are (usually) stored inside the PDF (exceptions are for instance fonts that can be retrieved from the operating system).
When you have an image that is shown on a PDF page, the bytes of that image are stored in what we call an Image XObject. An XObject is an object that is external to the page, but that is stored as a separate object inside the PDF file.
You are asking to serve the image bytes stored inside this separate object to a viewer on the operating system. This is impossible. I don't know of any viewer that can take those bytes and somehow forward them to an image viewer.
I can think of three possible workarounds. I don't know if any of these workarounds is acceptable to you.
1. Serve the image online
You could put the image on a server and use the code you have in your snippet to link to that online image. Of course: this will only work if the person viewing the document is online and clicks OK when his viewer asks him if it's OK to link to a resources on the internet.
2. Serve the image as an annotation
In this case, you create an annotation for which you create an appearance that renders that same image XObject in the annotation layer (all annotations are shown on top of the page content). You can easily change the visibility status of an annotation to make it invisible (in your case, this would be the default status) or visible (in your case, this would be triggered by a JavaScript action when clicking the link).
There's an example of such an annotation here: Advertisement. If you open advertisement.pdf, you see an image with a button that says "Close this advertisement". Once you click that, the status of the annotation will be changed to invisible. You could do something similar, but the other way round: click a link to make it visible instead of invisible.
This solution doesn't depend on an external viewer, the image is shown in the PDF viewer.
3. Add the image as optional content
Starting with PDF 1.5, PDF supports optional content. See for instance the OptionalContentExample. In this example, we have some questions and answers, but the answers are not visible by default. See layer_actions.pdf. There are links "on / off / toggle" to make the answers visible or invisible.
You could do the same with images: you could add them to a layer that is invisible by default, but that can be made visible if somebody clicks a link. However: this requires a viewer that supports OCG (optional content groups) and the actions to change the status of these OCGs. For instance: if you would try the layer_actions.pdf example in the PDF viewer in Chrome, it won't work, but if you download the PDF and open it in Adobe Reader, you'll see the behavior I described.
Summarized:
You are asking something that is impossible, but there are workarounds. Please post another question if you have chosen a workaround and you don't succeed in making that workaround word (but please take into account that not all viewers support every workaround).
no offence but too much knowledge sometimes makes you ignorant of small things.
simple solution to this problem is here
http://kuujinbo.info/iTextSharp/imageAnchor.aspx
sample code that i implemented works like charm
PdfPCell p1 = new PdfPCell();
p1 = new PdfPCell();
p1.Padding = 0;
p1.Border = 0;
PdfPTable nav = new PdfPTable(1);
nav.WidthPercentage = 100;
nav.SpacingAfter = 12;
navbarImg.Annotation= new Annotation(0, 0, 0, 0, ur);
p1.Image = navbarImg;
nav.AddCell(p1);
_doc.Add(nav);

pre-visualize an image

I'd like to pre-visualize an image in a image box before save it in a directory.
How can i do this, i use a checkbox to see if the user wants to pre-visualize or not because i dont find another way to do this without a checkbox.
I use file upload to upload the image.
string serverFileName = "";
serverFileName = Path.GetFileName(Upload.PostedFile.FileName);
Upload.PostedFile.SaveAs(MapPath("~/fotosPerfil/") + serverFileName);
i use this piece of code to save the image.
I think this post is exactly what you need to implement. Check out Ivan's solution.
Yes, indeed you can read the path selected by the user and display the image in an <img> tag, all client-side prior to uploading.

how to grab an image out of a DIV with C#

is it possible to grab an image that is embedded in a div via C# and attach it to an email?
I've got the code for attaching and sending images via email, I just don't know how to get it to grab the specific item out of the div and email it.
The image varies with a previous user selection so I can't just apply a static address of an image.
any ideas?
You need to grab image from inbound email?
Read more about MIME format: all images in email message are encriped in base64 format in MIME message. To grab your image you need to parse your emailmessage, read base64 string, wich encript your image, and decoding this string like this:
var byties = Convert.FromBase64String(Body)
I would look at using the microsoft html control, read the document and use the DOM to get the url of the image, download it using the http and then attach it to your email
for reference
How to download the image
Reading the Dom from visual studio
Use HtmlAgilityPack and HttpWebRequest.

How to use Image conrtrol in asp.net (C# web)

I have an image stored in my MS Access database with the data type OLE Object. I want it to display in an Image control. How can I do this? I tried this, but only in a PictureBox control in windows forms. Please help. Thanks in advance.
You need to create a page that can read the image from the database as binary data, then write that data directly to http response as binary data using Response.BinaryWrite to feed the image out. Then the src attribute of your image is pointed at the page itself like:
<img src="image.aspx" />
And the code behind image.aspx:
// An assumed method to get binary data our of the database
var bytes = YourDataLayer.GetBinaryImageData();
Response.Clear();
Response.AddHeader("Content-Disposition","attachment;filename=filename.jpg");
Response.ContentType = #"image\jpg";
Response.BinaryWrite(bytes);
Response.End();
read the image from the database as byte array and then create a temperary image object and assign it to the webcontrol on the page

Why are image acting as downloads?

I am using C# ASP.NET. I generate user friendly image names and use rewrite to find the correct image name. Normally in firefox when i right click an image and hit view image i get the image in my browser. However these images are acting like downloads, why?
global.asax:
void Application_BeginRequest(Object sender, EventArgs e)
{
lazy(Context, HttpContext.Current.Request);
}
file.cs:
void lazy(...)
{
...
context.RewritePath(sz);
//sz = "/user/username/type/image.png"
}
Likely because the correct MIME type is not being sent along with the image.
The mime-type of the image is probably being reset by the RewritePath call. If so, FireFox then thinks that the image is just a binary stream and doesn't know what to do with it so it just attempts to download it.

Categories