How to assign Bitmap to ImageControl - asp.net

I am trying to bind image to image control, but image is bitmap data. How can I?

Do you mean that you have an image as binary data and you want to display it without saving it in a file? If yes, then one solution will be to create a separate aspx page or an http handler to return the image. You need to set the content type and length accordingly and use Response.BinaryWrite for serving the image.
See here for some more suggestions.

Related

Display resized image as a return value and not as page content

On Page_Load, I want the page to display an image the same way it displays when you "Open image in a new tab". In other words, I want the page to display as content type Image/jpeg instead of rendering the image in img HTML tag.
I successfully displayed an image this way using the below code on Page_Load:
Page.Response.ContentType="image/jpeg"
Page.Response.WriteFile("Path_to_the_Image")
However, my case requires displaying the image after being resized. I created a function that resizes the image and returns the resized image as Drawing.Bitmap
ResizeImage(bmSource As Drawing.Bitmap, TargetWidth As Int32, TargetHeight As Int32)
How can I use the returned value of that function and display it the same way I displayed the first image?
Page.Response.WriteFile() requires a String as an input. Is there a way where I can display the image of type Image or System.Bitmap directly into the page without having to save it in a temp folder and then use its path (As a String) and pass it to Page.Response.WriteFile?
Page.Response.ContentType="image/jpeg"
Using resizedImage As Bitmap = ResizeImage(source, 100, 100)
source.Dispose()
resizedImage.Save(Page.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
End Using
Page.Response.Flush()
That should work, maybe there's some error in my syntax. I do not often code in vb.net so.
Edited the code because of #Joel Coehoorn note

Copy images from imagefield

I currently have a pdf that contains imagefields.
The issue however is that we want to be able to copy the images users input into the pdf.
Is there a way to alter the imagefield or add a button to the form that will copy the image so it will be available for pasting?
I see that I can put a button on the page and via script access the value for ImageField1.value so I can set it. But how can I read it?
you can access the image data by ".rawValue" attribute.
For example with a button and "click" event.
FormCalc: $.parent.sPic.rawValue = $.parent.dPic.rawValue
JavScript: xfa.resolveNode("sPic").rawValue = xfa.resolveNode("dPic").rawValue
with dPic being imagefield and sPic being image. This script will copy the image from imagefield into the image.

Where to store and how to remove images in a photo gallery?

I'm working on a photo gallery using ASP.NET.
I'm storing user's images in a SQL database. I'm not sure how should displaying images look like.
Let's say there is 1 picture per user, I was doing something like that:
get image from database
save it on server's disc as "file.jpg"
ASP:Image.uri = "file.jpg"
And that worked fine until I found out that If few users loads that page at the very same time, It might not work properly.
Then I though changing "file.jpg" into some random string would help me:
get image from database
save it on server's disc as "ABCDUDHDJSAKFHADGJKHAKADFAD.jpg"
ASP:Image.uri = "ABCDUDHDJSAKFHADGJKHAKADFAD.jpg"
File.Delete("~/ABCDUDHDJSAKFHADGJKHAKADFAD.jpg");
But it wasnt possible to delete this file because it was still being used by a server.
What would be the proper way to solve my problem? User in my photo gallery will eventually see 12 photos at the same time.
What I usually do is serve images from DB using a generic handler (ashx file).
What you need to do is create an ashx file and use something along these lines:
context.Response.BinaryWrite(myImage);
The you create image tags like so:
<img alt="whatever" src="/images.ashx?iid=1243 />
Where iid is the unique ID of the image in the DB.
You should not store the file actually. You should stream it. For example create a handler which returns the image from an user with a specific ID and call it with that ID like image.ashx?id=1. The handler could look like
Image image; // image from your database
using(MemoryStream ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.WriteTo(context.Response.OutputStream);
}
Response.ContentType = "image/png";
This handler you can use like a static image file. So, you might even use it for a background-image:
<div style="background-image: url(image.ashx?id=1)">Username</div>

What could cause this image to not display?

I have an ASP.NET web app. What I am attempting should be simple: after an image is displayed, I have a Rotate button which should allow the user to rotate the image 90 degrees. Here is the button click code...
Dim i As Image
i = Image.FromFile("C:\Inetpub\wwwroot\myWebApp\MyImage.jpg")
'rotate the picture by 90 degrees
i.RotateFlip(RotateFlipType.Rotate90FlipNone)
're-save the picture as a Jpeg
i.Save("C:\Inetpub\wwwroot\myWebApp\MyImage.jpg",System.Drawing.Imaging.ImageFormat.Jpeg)
'tidy up after we've finished
i.Dispose()
The image and button used here are non-remarkable. When I created a sample app with just 1 page this works perfectly. However, when I put in into my main app, even if its in a new page, all by itself with nothing else, not even a masterpage, it does, in fact rotate the image and write it back to the file system, but it doesn't show the rotated file. It shows the image as it was. UNTIL I hit F5, then, no matter how many times I hit the button, it works perfectly. I have tried eveything i can think of to clear the cashe to no avail.
Are you re-requesting the image using an update panel or some other mechanism? You will be able to force a refresh by putting something like a timestamp or similar GET parameter on the IMG src, or in the ASP:Image source.
Instead of saving the image back to the web root you can keep the original image as is and just stream the rotated image to the client.
<img src="RotateImage.aspx?Image=MyImage.jpg&Rotate=90" />
and then in your RoateImage.aspx do the same load/rotate work and:
i.Save(Response.OutputStream, ImageFormat.Jpeg)
Response.ContentType = "image/jpeg"
The image is served from the cache, that is why it is not shown updated. To show the newer version of the same image. You may attach a query string to the path of the image. For example,
<img src='/images/image.jpg?<%= DateTime.Now.Ticks %>' />

How can I set an Image received from a method to the ImageButton?

I have a method that returns an Image.
How can I assign that Image to my ImageButton control so it has that image set?
Since you're dealing with HTML, you'll need to save the Image to a file, then use that file in the ImageButton's ImageUrl property.
I believe an ImageButton takes the path to an Image, not an actualy Image object, as the browser renders it a an tag.
What you could do is save thr image to disk, return the path to the image form your method and then have
<asp:ImgageButton id="imgButton1" runat="server" imageUrl="<%= GetImageUrl()>" />
The above syntax is not exact, it might be "<% Response.Write(GetImageUrl())>" but you get the picture
If this method returns an Image object, you will need to save out the image to a physical location on your webserver (somewhere like Server.MapPath("~/images/")). Then you will need to set the ImageUrl property of the ImageButton control to this location.
If this method returns a relative path to the image file, simply set the ImageUrl property of the ImageButton to the path returned by the method.
In short, you can't. Well not directly anyway.
You will have to write your image to file and point the image button at your image file
or you can have a web page that returns the image in the response and use that as your ImageUrl
The reason for this is that your ImageButton just renders to HTML which has no support for attaching images ATM.
You can try the following approach:
1) Firstly, get the binary data from the image:
public byte[] ImageToByteArray(System.Drawing.Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
2) Secondary use Inline Images with Data URLs idea:
Inline images use the data URI scheme
to embed images directly within web
pages. As defined by RFC 2397, data
URIs are designed to embed small data
items as "immediate" data, as if they
were referenced externally. Using
inline images saves HTTP requests over
externally referenced objects.
System.Drawing.Image image = GetImageFromSomewhere(...);
byte[] imageData = ImageToByteArray(image);
string imageBase64 = Convert.ToBase64String(imageData);
string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
imgButton.Src = imageSrc;
But unfortunately, this won't work for IE5-7 (should work in IE8).

Resources