Mohamed Mahir
Mohamed Mahir

Reputation: 91

unable to copy clipboard to C:\ folder

I am trying to take a screenshot of my monitor, after pressing Print Screen button. With the code below I am trying to check whether there is any contents in clipboard. If so, I am trying to save it in c:. When i run the below code, it always results in "A generic error occurred in GDI+." exception. but when i use code but change the folder to "f:\" then its working fine. It looks like access permission issues with "C:\" folder. Please let me know how to enable permissions for C folder.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace Example2
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            if (Clipboard.ContainsImage())
            {
                Image screenshot = Clipboard.GetImage();
                Image mahir = screenshot;

                mahir.Save("c:\\screenshot.gif", System.Drawing.Imaging.ImageFormat.Gif);
            }
        }
    }
}

Upvotes: 0

Views: 125

Answers (1)

Grant Winney
Grant Winney

Reputation: 66439

Why save it to c: at all, and not somewhere more manageable, like My Documents or Desktop? Depending on where you run your program, you may or may not run into security issues trying to save directly to c:.

Consider using the Environment.SpecialFolder enum to get a proper location and save the image there.

mahir.Save(Path.Combine(
  Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "screenshot.gif"),
  System.Drawing.Imaging.ImageFormat.Gif);

Alternatively, you could try running your program with admin privileges.

Upvotes: 3

Related Questions