Liu Jia Hui
Liu Jia Hui

Reputation: 231

How to set Image Web Control into Bitmap in ASP.NET?

I would like to know how can I set a Bitmap from a Image Web Control? The Image Web Control named is imgLoader. I have tried

Bitmap bmp = new Bitmap(imgLoader);

However, the error stated that it:

Cannot convert from 'System.Web.UI.WebControls.Image' to 'System.Drawing.Image' and 'The best overloaded method match for 'System.Drawing.Bitmap.Bitmap(System.Drawing.Image') has some invalid arguments.'

Upvotes: 0

Views: 4647

Answers (4)

Hesham
Hesham

Reputation: 371

The Image WebControl will emit HTML when page renders in client side. The HTML element will have its src attribute pointing to the url of the image so that the browser can download and display in its allocated place.

If you want to load the same image in Bitmap object, get the physical path of the image in your web server and create the bitmap as below

Bitamp bmp = Bitmap.FromFile("PHYSICAL-IMAGE-PATH");

You can get the physical image path from the ImageUrl property and convert the url to physical absolute path through Server.MapPath.

If the image url is not stored locally in your server, you can download the image using the HttpClient and saved under your server TEMP folder to be able to load and manipulate.

Upvotes: 0

Sain Pradeep
Sain Pradeep

Reputation: 3125

Source

They are two completely different objects:

  • System.Web.UI.WebControls.Image is a control that has the ability to render HTML which will make the browser download and display an appointed image

  • System.Drawing.Image is a class that has the ability to load an image into memory for manipulating it, or to display it in a control
    (but not the web image control).

So unfortunately there is no way you can convert a System.Web.UI.WebControls.Image to a System.Drawing.Image; it doesn't even touch the image data.

If you would like to take the image at the ImageUrl and convert it to a System.Drawing.Image you can call

System.Drawing.Image.ImageFromFile("path/to/image")

Upvotes: 1

Hans Derks
Hans Derks

Reputation: 1027

Maybe you can try

Bitmap bitmap = new Bitmap(Server.MapPath(imgLoader.ImageUrl));

Upvotes: 2

user2815602
user2815602

Reputation:

Bitmap constructor need image as a argument.

Casting the "imgLoader" image to System.Drawing.Image and pass the image to Bitmap.

Upvotes: 0

Related Questions