© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
7 replies
Myvik_

❔ "A General Error Has Occurred in GDI+"

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;
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ A generic error occurred in GDI+.' -> Save Image Path
C#CC# / help
2y ago
Resending confirmation email results in "A failure has occurred" error
C#CC# / help
2y ago
❔ ✅ An unhandled exception has occurred
C#CC# / help
3y ago