C
C#2y ago
Myvik_

❔ "A General Error Has Occurred in GDI+"

c++
int leftOffset = int.Parse(LeftOffset.Text);
int topOffset = int.Parse(TopOffset.Text);

int width = int.Parse(RadarWidth.Text);
int height = int.Parse(RadarHeight.Text);

Image screen = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(screen))
{
g.CopyFromScreen(leftOffset, topOffset, 0, 0, new System.Drawing.Size(width, height));
g.Dispose();
}

string imagePath = $"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\Screen.jpg";
screen.Save(imagePath);
screen.Dispose();

BitmapImage bitmapImage = new BitmapImage();

bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.UriSource = new Uri(imagePath);
bitmapImage.EndInit();

ScreenshotImage.Source = bitmapImage;
c++
int leftOffset = int.Parse(LeftOffset.Text);
int topOffset = int.Parse(TopOffset.Text);

int width = int.Parse(RadarWidth.Text);
int height = int.Parse(RadarHeight.Text);

Image screen = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(screen))
{
g.CopyFromScreen(leftOffset, topOffset, 0, 0, new System.Drawing.Size(width, height));
g.Dispose();
}

string imagePath = $"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\Screen.jpg";
screen.Save(imagePath);
screen.Dispose();

BitmapImage bitmapImage = new BitmapImage();

bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.UriSource = new Uri(imagePath);
bitmapImage.EndInit();

ScreenshotImage.Source = bitmapImage;
If I delete 21 lines, an exception occurs, which is indicated in the topic name. If I do not remove it, then the screenshot is displayed in the application 1 time and when I try to take a screenshot again, it is saved on the desktop, but it is not displayed in the application. How to fix it? It is not possible to use the using directive.
4 Replies
Myvik_
Myvik_OP2y ago
Decided. Decision:
c++
ScreenshotImage.Source = null;

BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bitmapImage.UriSource = new Uri(imagePath);
bitmapImage.EndInit();

ScreenshotImage.Source = bitmapImage;
c++
ScreenshotImage.Source = null;

BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bitmapImage.UriSource = new Uri(imagePath);
bitmapImage.EndInit();

ScreenshotImage.Source = bitmapImage;
Omnissiah
Omnissiah2y ago
why are you calling g.Dispose() inside the using the aim of using is calling Dispose() for you also i don't have time to check now but it seem there is something off with the sequence of the operations
Myvik_
Myvik_OP2y ago
why are you calling g.Dispose() inside the using
I was already tired at that time and for some reason I didn't notice it Everything works fine for me
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server