recently, I've been developing a website with .NET c# and I've been trying to display images via the imagepath in the db. It is currently working by this line of code.
return File(byte array, "image/jpeg");
The problem is that by this line of code the layout page is completely ignored as only the image with white background is shown. I need to display the image along with the return view (the image inside the layout of the website). thx
The reason you're not seeing anything is because you can only have one type of response per request; Either a response containing HTML or a response containing the image.
What you need to do in order to serve images via your application is set up a route that you can put in your <img> tags as the src attribute.
<img src="/static/images/123">
Your route would listen for requests on the /static/images/ path and then try to parse the ID number at the end. It could then take that ID number (123) and look up the relevant image in your database.
So, to be clear, you'd have at least two requests that occur; First, you serve the request for the page, then you serve subsequent requests for the image(s). These two request handlers do not share the same code.
Finally, if you really wanted to "inline" an image as part of the page response, The only way you can "inline" an image into a page is to base64 encode it and set that as as the src of an <img> tag. This process is slow and bloats your HTML, making it take longer to load.
Related
Fairly straightforward question, I have images stored in my database as varbinary and would like to provide a link to these images rather than displaying them on the website. When a user clicks the link he/she should be able to download the image.
An image is served in response to a request.
<img src="?" /> <!-- what goes here? -->
You need to create an HTTP handler to receive requests for these images.
A handler is an executable available at a specific URL which can respond to your request; in this case, by serving the binary data of an image. An ASPX page is a valid handler, though there are more efficient handler types for images.
<img src="MyHandler.aspx?imageId=123" />
The handler should do a few things:
validate that the ID is valid and that the caller has permissions (if needed)
retrieve the image from the database
set appropriate response headers
use Response.BinaryWrite() to send the binar data to the client.
Note that if you are using ASP.Net MVC, you can use a controller as your handler.
An alternative method is to base 64 encode the bytes and embed them directly in the image tag.
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
This is a useful technique for small images or when the expense of multiple requests is very high.
See: http://en.wikipedia.org/wiki/Data_URI_scheme
More reading:
Dynamically Rendering asp:Image from BLOB entry in ASP.NET
Dynamic Image Generation
You should create a page that takes an image-id and returns the image.
You may consider this question:
Display image from a datatable in asp:image in code-behind
I had a similar problem an that answer solved my question. Beside that, you can use binaryimage component from Telerik:
http://www.telerik.com/products/aspnet-ajax/binaryimage.aspx
I am developing a C# web project. I run it on the local web server.
I draw. I show image as follows:
bitmap.Save(Server.MapPath("diagram.jpg"), ImageFormat.Jpeg);
Image1.ImageUrl = ResolveUrl("diagram.jpg");
I don't see new image. Only old one, which I had after changing image name
(Say, I change diagram.jpg to diagram2.jpg).
Browser is Firefox.
The design page in C# is simple. Just Image and few TextBoxes on the page.
No UpdatePanel and such.
Something with caching... But how to fight with that...
But how to fight with that.
Always use a separate path / name. Pug a GUID somewhere. Simple like that. Different file can not be cached.
I'm not sure what ResolveUrl does, but try adding a querystring to the image url so that the page always gets a "fresh" file. Something like this:
Image1.ImageUrl = ResolveUrl(string.Format("diagram.jpg?v={0}", Guid.NewGuid()));
You can alternative write the image file as
diagram.jpg?ver=2
to keep the same image file, but force the browser to update it.
If Image has Same name and URL browser picks the image from the cache and displays the same for faster loading of the pages.
Even if you change the image server side the same cached image is displayed until you clear the cache of the browser. You can use query string to change image url like below.
Image1.ImageUrl = ResolveUrl("diagram.jpg?" + DateTime.Now.Ticks.ToString());
I've been reading through a few asp.net articles, and attempting some code, but I think I may be confused. Can you or can you not draw lines on the screen with code on a ASP.NET webform using c#?
If so, can anyone direct me to some examples?
You cannot directly draw on a webform. You may draw on the image and then embed it on your webform (like any other image).
You can make a canvas and then you can draw whatever you want to draw on it.But direct drawing is impossible.
If you are fine with HTML5 you can try lineto Javascript method:
<script>
context.lineTo(100, 200);
</script>
Please refer # following link for more details:
http://www.html5canvastutorials.com/tutorials/html5-canvas-lines/
Not sure what you mean by "draw a line" but if you are using a web browser you need some kind of HTML object to display this "line". If all you need is a horizontal line you can just add a HR html tag and use CSS toy stylize it. You can also include this line in an image or HTML5 Canvas.
Because there is not screen for your server side code. Your code generates HTML, JavaScript etc. and then browser uses that content to render it on the client screen. So your options are to generate image (draw all that you need) in server side code and send it to the browser, or you can use JavaScript and send browser instructions how to draw lines.
You could probably create a new image using the System.Drawing namespace classes and then do something like dynamically load it into an <img /> tag...but depending on what you're trying to accomplish it may be far easier to either use a JavaScript library of some sort or to use some sort of very simple line image and tweak the length/height using css.
More detail would be needed to understand what you're trying to do. As others have pointed out though, there's no direct way for your C# code to interact with the page. You'd have to have something on the page like an img tag and then set it's source to a C# file like a handler (.ashx). In that handler you could generate the image and then set the response content type as image/jpg and write the raw bytes to the response stream...
Again though, that seems like overkill for something that could be accomplished with CSS or javascript.
Hi I tried to read a page using HttpWebRequest like this
string lcUrl = "http://www.greatandhra.com";
HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
loHttp.Timeout = 10000; // 10 secs
loHttp.UserAgent = "Code Sample Web Client";
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream(), enc);
string lcHtml = loResponseStream.ReadToEnd();
mydiv.InnerHtml = lcHtml;
// Response.Write(lcHtml);
loWebResponse.Close();
loResponseStream.Close();
i can able to read that page and bind it to mydiv. But when i click on any one of links in that div it is not displaying any result. Because my application doesnt contain entire site. So what we will do now.
Can somebody copy my code and test it plz
Nagu
I'm fairly sure you can't insert a full page in a DIV without breaking something. In fact the whole head tag may be getting skipped altogether (and any javascript code there may not be run). Considering what you seem to want to do, I suggest you use an IFRAME with a dynamic src, which will also hopefully lift some pressure off your server (which wouldn't be in charge of fetching the html to be mirrored anymore).
If you really want a whole page of HTML embedded in another, then the IFRAME tag is probably the one to use, rather than the DIV.
Rather than having to create a web request and have all that code to retrieve the remote page, you can just set the src attribute of the IFRAME to point ot the page you want it to display.
For example, something like this in markup:
<iframe src="<%=LcUrl %>" frameborder="0"></iframe>
where LcUrl is a property on your code-behind page, that exposes your string lcUrl from your sample.
Alternatively, you could make the IFRAME runat="server" and set its src property programatically (or even inject the innerHTML in a way sismilar to your code sample if you really wanted to).
The code you are putting inside .InnerHtml of the div contains the entire page (including < html >, < body >, < /html > and < /body> ) which can cause a miriad of problems with any number of browsers.
I would either move to an iframe, or consider some sort of parsing the HTML for the remote site and displaying a transformed version (ie. strip the HTML ,BODY, META tags, replace some link URLs, etc).
But when i click on any one of links in that div it is not displaying any result
Probably because the links in the download page are relative... If you just copy the HTML into a DIV in your page, the browser considers the links relative to the current URL : it doesn't know about the origin of this content. I think the solution is to parse the downloaded HTML, and convert relative URLs in href attributes to absolute URLs
If you want to embed it, you need to strip everything but the body part. That means that you have to parse your string lcHTML for <body....> and remove everything before and includeing the body tag. You must also strip away everything from </body>. Then you need to parse the string for all occurences of <a href="....."> that do not start with http:// and include h t t p://www.greatandhra.com or set <base target="h t t p://www.greatandhra.com"> in your head section.
If you don't want to embed, simply clear the response buffer and stream the lcHTML string back to the browser.
PS: I had to write all h t t p with spaces to be able to post this.
Sounds like what you are trying to do is display a different site embedded in your site. For this to work by dropping it into a div you would have to extract the code between the body tags as it wouldn't be valid with html and head in the middle of another page.
The links won't work because you've now taken that page out of context in your site so you'd also have to rewrite any links on the page that are relative (i.e. don't start with http) to point to a page on your site which will then fetch the other sites page and display them back in your site, or you could add the url of the site you're grabbing to the beginning of all the relative links so they link back to that site.
I am loading a css file dynamically by making the link style attribute a server tag.
All of the css loads fine except for the image. It just shows the alternate text. I am doing this in the page_load event.
Here is a snippet of my img markup:
<img class="logourl" alt="Header" />
Here is a the css for logourl:
.logourl
{
background-image:url(../images/a-logo.png);
width:169px;
height:61px;
margin-top:5px;
}
When I right-click on the image and view properties, it is blank (size, address, etc).
<img class="logourl" alt="Header" />
Can an <img> tag have a background-image property???? That doesn't even make sense.
According to MSDN, the img object doesn't have this property, so I'd say it's safe to assume it's not supported in IE.
Are you sure you don't want a span, a hyperlink, or some other property where a background-image would make sense?
edit
I just read #Justin Grant's answer. I guess that you can use a background image on an image tag, but I'm keeping my answer because it seems a silly way to do it. if what you want is a frame around your image, I would create a div or span with a set width and height, and an img slightly smaller inside it.
I wouldn't call that loading the CSS file dynamically. You are setting the attribute in the link tag dynamically, but the CSS file is loaded the same way as any other CSS file. The browser doesn't see any difference from any other link tag, so the dynamically set url is not part of the problem.
Where are the image and the css files located? Remember that the url is relative to where the CSS file is, not where the page is.
Note that even if you mangage to set the background image for the image, it will still show the alt text on top of the image and indicate that the image is not loaded. The background-image attribute does not set the source of the image. You should just use a div element instead of the image element.
To put a background-image on an IMG tag, according to http://www.contentwithstyle.co.uk/content/css-background-image-on-html-image-element you'll need to apply a CSS padding and make sure you're using display:block.
Also, if this is a directory-path issue, the image must be relative to the location of the CSS file it's referenced from. Try using an absolute path to verify that this is indeed the problem, then experiment with various relative paths to see if that it gets solved.
If it's still unsolved at that point, I'd suggest trying Fiddler (www.fiddlertool.com) which will let you see HTTP requests being made-- specifically the actual URL being requested by your browser. Make sure to clear your cache first before reloading the page under Fiddler, so you'll be sure to actually make the request and not pull from cache. Anyway, there will be three possible cases:
Fiddler does not show you requesting a URL at all. This points to some issue with your CSS (e.g. not being loaded properly, or a syntax error)
Fiddler shows the image URL being requested, but it shows a 404 (not found) or other error. this means the wrong URL is being requested, or there's a problem with your web site's serving of images from that folder
Fiddler shows the correct URL, and it's getting a 200 status (success) from the server. If this happens, I'm stumped!
Right-click will only show you SRC of an IMG tag. when using a background CSS, you'll need view source.