-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);
}
3

4 Answers 4

1

You can always use the AppData folder,

string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
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
     }
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.