using (System.IO.FileStream fs = File.Open(GetCurrentWallpaper(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
I am writing an application that needs to open the current wallpaper every time, how it happens. First, I access the registry to get the path to the wallpaper (GetCurrentWallpaper), and use FileSystemWatcher to create the wallpaper on change. Oddly enough, it only works once. If the wallpaper is accessed a second time (no matter how long I wait), my application crashed with an IOException, which said that I could not access the file because it is already in use. If I restart the application, it will again be able to access the file, but, as mentioned above, only once. Otherwise, it falls. Is there anything I can do to access this file?
Edit: More code:
using (System.IO.FileStream fs = File.Open(GetCurrentWallpaper(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
using (Bitmap orig = (Bitmap)Bitmap.FromStream(fs, true, false)) {
int width = Convert.ToInt32(orig.Width / 3);
int height = Convert.ToInt32(orig.Height / 3);
Rectangle rect = new Rectangle(0, 0, width, height);
using (Bitmap bmp = new Bitmap(width, height)) {
using (Graphics bmpg = Graphics.FromImage(bmp)) {
col = ColorHelper.CalculateAverageColor(bmp, true, 20);
fs.Flush();
fs.Close();
}
}
}
}
public static System.Drawing.Color CalculateAverageColor(Bitmap bm, bool dropPixels, int colorDiff) {
int width = bm.Width;
int height = bm.Height;
int red = 0;
int green = 0;
int blue = 0;
int minDiversion = colorDiff;
int dropped = 0;
long[] totals = new long[] { 0, 0, 0 };
int bppModifier = bm.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? 3 : 4;
BitmapData srcData = bm.LockBits(new System.Drawing.Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, bm.PixelFormat);
int stride = srcData.Stride;
IntPtr Scan0 = srcData.Scan0;
unsafe {
byte* p = (byte*)(void*)Scan0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int idx = (y * stride) + x * bppModifier;
red = p[idx + 2];
green = p[idx + 1];
blue = p[idx];
if (dropPixels) {
if (Math.Abs(red - green) > minDiversion || Math.Abs(red - blue) > minDiversion || Math.Abs(green - blue) > minDiversion) {
totals[2] += red;
totals[1] += green;
totals[0] += blue;
} else {
dropped++;
}
} else {
totals[2] += red;
totals[1] += green;
totals[0] += blue;
}
}
}
}
int count = width * height - dropped;
int avgR;
int avgG;
int avgB;
if (count > 0) {
avgR = (int)(totals[2] / count);
avgG = (int)(totals[1] / count);
avgB = (int)(totals[0] / count);
} else {
avgR = (int)(totals[2]);
avgG = (int)(totals[1]);
avgB = (int)(totals[0]);
}
bm.UnlockBits(srcData);
return System.Drawing.Color.FromArgb(avgR, avgG, avgB);
}