0

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.'

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Liu Jia Hui
  • 231
  • 2
  • 8
  • 16

4 Answers4

2

Maybe you can try

Bitmap bitmap = new Bitmap(Server.MapPath(imgLoader.ImageUrl));
Hans Derks
  • 1,027
  • 7
  • 7
1

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")
Community
  • 1
  • 1
Sain Pradeep
  • 3,119
  • 1
  • 22
  • 31
0

Bitmap constructor need image as a argument.

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

0

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.

Hesham
  • 361
  • 2
  • 11