C#C
C#3y 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;


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.
Was this page helpful?