2014-06-18 130 views
-1

在我的方法中,我试图将图像保存在项目目录中的文件夹中。我试过只是放置文件夹的直接文件路径,但是当项目运行时会给我一个错误。将图像保存到asp.net/c#中的项目文件夹中

在c#中有一种内置的扩展,它允许我将这个图像保存在我的目录中的一个文件夹中;或者只是简单地访问我的目录而不钻取我的项目保存在我的计算机上的位置?

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 [email protected]"images/image1.jpg"; 

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

你会得到什么错误? – Divi

+0

重复? [将文件保存在我的项目中的指定文件夹](http://stackoverflow.com/questions/14788217/saving-a-file-in-a-specified-folder-inside-my-project) –

+0

Try Server.MapPath ( “图像/” +文件名) –

回答

1

您可以随时使用AppData文件夹,

string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 
0

我可以猜测有图像文件的文件名相似 尝试把它用不同的名称

像 字符串的文件路径= @“图像/布拉布拉或AI.jpg”;

1

假设“图片”文件夹是在项目中使用的根目录:

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

你可以检查文件是否已经在一个位置存在:

if (!File.Exists(filePath)) 
    { 
     // Your code to save the file 
    } 
0

使用此

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

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

相关问题