2

I'm new to using c# with asp.net ajax so any help is appreciated. I am using System.Drawing.Image to get my image's virtual path with Image.FromFile() and want to return the image to be set up in my web application. Thanks in advance for any response!

I have created a physical path then converted it to a virtual path then used Server.MapPath to get the equivalent path to assign to my Image image.

string pImagePath = @"C:\Apps\WebApp.Roll\Images\Profiles\" + item.Value.ToString();
string vImagePath = pImagePath.Replace(@"C:\Apps\WebApp.Roll", "~").Replace(@"\", "/");
Image image = Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(vImagePath), true);

#item.value is name of the image file.

My path is correct but whenever I debug to see what my Image image is, it tells me that my image is System.Drawing.BitMap What am I doing wrong?

Rae
  • 25
  • 10
  • I'm confused. If you didn't expect the `Image.FromFile()` method to return a memory-loaded bitmap, what did you expect? – gunr2171 Jun 10 '19 at 15:52
  • My image is a jpg, do I need to use a converter? Or should I be using Graphics.DrawImage because right now when I load my WebApplication I get back System.Drawing.BitMap. – Rae Jun 10 '19 at 15:52
  • @Rae no https://stackoverflow.com/a/330350/1043380 – gunr2171 Jun 10 '19 at 15:53
  • 1
    Possible duplicate of [C# read a JPEG from file and store as an Image](https://stackoverflow.com/questions/330346/c-sharp-read-a-jpeg-from-file-and-store-as-an-image) – gunr2171 Jun 10 '19 at 15:54
  • 1
    It's not clear at all from the question what the problem is here. Are you unable to return the `Image`? What error are you getting? Or perhaps you're confusing the `System.Drawing.Bitmap` class with the `BMP` file extension? They are two different things, you know. Please read [this](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap?view=netframework-4.8) for more information. – Rufus L Jun 10 '19 at 15:55
  • I have look at this link but I still don't understand why I can't get my image to stay as jpeg? – Rae Jun 10 '19 at 15:56
  • @Rae [`System.Drawing.Bitmap`](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap?view=netframework-4.8) is a class, not a file format. It represents the bits that form your image. – Rufus L Jun 10 '19 at 15:58
  • Thank you! @Rufus L – Rae Jun 10 '19 at 16:00
  • It's not clear where `item.Value` comes from, but if it's from the browser, beware of [path traversal attacks](https://www.owasp.org/index.php/Path_Traversal). Check that the resulting filepath is under the directory that you expect it to be. – spender Jun 10 '19 at 16:10

1 Answers1

2

In the case of C# code, the class Bitmap refers to any image that uses pixel arranged colors into a map of bits. This is not limited to files with the extension .bmp (Windows Bitmap). Examples that are Bitmaps: JPG, PNG, GIF, BMP.

As opposed to image files that contain a set of instructions to build the image such as SVG (Scalable Vector Graphics) or WMF (Windows Metafile Image). These are examples of images that are not Bitmaps.

Rhaokiel
  • 813
  • 6
  • 17
  • Thanks for explaining. I see now that Bitmap includes JPG. I marked this as the answer as soon as it lets me! is there a way to load my bitmap to an actual image? – Rae Jun 10 '19 at 15:59
  • 1
    As the documentation shows (https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap) the class Bitmap overrides the class Image. So any System.Drawing.Bitmap instance will work as the type System.Drawing.Image in any context. – Rhaokiel Jun 10 '19 at 16:02
  • Thank you @Rhaokiel – Rae Jun 10 '19 at 16:10