-1

In my method, I'm trying to save an image in a folder in the directory of my project. I have tried just putting the direct filepath of the folder, but that gives me an error when the project runs.

Is there a built-in extension of some kind in c# that would allow me to save this image in a folder in my directory; or way to simply access my directory without drilling to where my project is saved on my computer?

private void CreateBarcode()
{
    var bitmapImage = new Bitmap(500,300);
    var g = Graphics.FromImage(bitmapImage);

    g.Clear(Color.White);
    UPCbarcode barcode = new UPCbarcode(UPCbarcode.RandomGeneratedNumber(), bitmapImage, g);
    string filepath=@"images/image1.jpg";

    bitmapImage.Save(filepath,System.Drawing.Imaging.ImageFormat.Jpeg);
}
Divi
  • 7,621
  • 13
  • 47
  • 63
user3750761
  • 27
  • 1
  • 1
  • 8

4 Answers4

1

Assuming the "Image" folder is in the root directory of your Project use:

Server.MapPath("~/Image" + filename)

you can check if the file already exist at a location by :

if (!File.Exists(filePath))
     {
         // Your code to save the file
     }
Netnetter
  • 41
  • 1
  • 9
1

You can always use the AppData folder,

string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
JOAT
  • 188
  • 7
0

I can guess that there is similarity in the name of the image file try putting it under a different name

like string filepath = @ "images / blabla or AI.jpg";

0

Use this

string filepath= Application.StartupPath + "\images\image1.jpg";

bitmapImage.Save(filepath,System.Drawing.Imaging.ImageFormat.Jpeg);

Nitish Katare
  • 1,147
  • 2
  • 12
  • 22