开发者

Permission problem with bitmap.Save()

I have this simple code:


System.Drawing.Bitmap bm = bitmapSourceToBitmap(source);
try
            {
                bm.Save(@"C:\Seva\testeImagem.jpg");
            }
            catch (Exception ex)
            {

            }

This throws: Generic Error GDI+. Anyway, I seached and people say that the problem is wit开发者_如何学运维h permissions. How can I give permissions to it? Thanks


First find out under what credentials the code is running.

Then check (and, when needed, fix) the security/NTFS settings of the Seva folder.

Especially when this code is running from within a website or service the account will not have permissions to write to the folder.


instead of saving to C:\Seva\testeImagem.jpg why not try saving to

Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), 
             "testeImagem.jpg");

You must ensure that the Seva folder exists under C:\ and ensure that the current user has permissions to write to\create this folder. Also, its considered bad practice to write to folders that the user doesn't own. If the user is Running As A Normal User (not an admin) failure to do so results in permission exceptions.


Could you test if the folder exists?

void BitmapCopy(System.Drawing.Bitmap source, string filename) {
  if (!String.IsNullOrEmpty(filename) && (source != null)) {
    string dirName = @"C:\Seva";
    if (!System.IO.Directory.Exists(dirName)) {
      dirName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    }
    string bmpFile = System.IO.Path.Combine(dirName, filename);
    System.Drawing.Bitmap bm = bitmapSourceToBitmap(source);
    try {
      bm.Save(bmpFile);
    } catch (ArgumentNullException ex) {
      Console.WriteLine(ex.Message);
    } catch (System.Runtime.InteropServices.ExternalException ex) {
      Console.WriteLine(ex.Message);
    }

  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜