Myvik_
Myvik_
CC#
Created by Myvik_ on 5/25/2023 in #help
❔ "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.
8 replies