2014-06-18 126 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);

相關問題