Ошибка "A generic error occurred in GDI+" - C#
Формулировка задачи:
В сишарпе не силен, что не так?
ошибка:
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
Additional information: A generic error occurred in GDI+.
в этой строке:
код:
Листинг программы
- tempImage.SaveAsFile(@"C:\full.png", ImageFormat.Png);
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Threading.Tasks;
- using OpenQA.Selenium;
- using OpenQA.Selenium.Firefox;
- using OpenQA.Selenium.Chrome;
- using OpenQA.Selenium.Support.UI;
- using OpenQA.Selenium.Remote;
- using OpenQA.Selenium.Interactions;
- using DeathByCaptcha;
- using System.IO; // для работы с файлами
- using System.Threading; // для пауз
- using System.Net; // для скачки капчи
- using System.Diagnostics; // для отслеживания процессов
- namespace driver
- {
- class Program
- {
- public static Bitmap CropImage(Bitmap source, Rectangle section)
- {
- Bitmap bmp = new Bitmap(section.Width, section.Height);
- Graphics g = Graphics.FromImage(bmp);
- g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
- return bmp;
- }
- static void Main(string[] args)
- {
- IWebDriver driver = new ChromeDriver();
- //replace with the page you want to navigate to
- string your_page = "https://www.google.com";
- driver.Navigate().GoToUrl(your_page);
- ITakesScreenshot ssdriver = driver as ITakesScreenshot;
- Screenshot screenshot = ssdriver.GetScreenshot();
- Screenshot tempImage = screenshot;
- tempImage.SaveAsFile(@"C:\full.png", ImageFormat.Png);
- //replace with the XPath of the image element
- IWebElement my_image = driver.FindElement(By.XPath("//*[@id="hplogo"]/canvas[1]"));
- Point point = my_image.Location;
- int width = my_image.Size.Width;
- int height = my_image.Size.Height;
- Rectangle section = new Rectangle(point, new Size(width, height));
- Bitmap source = new Bitmap(@"C:\full.png");
- Bitmap final_image = CropImage(source, section);
- final_image.Save(@"C:\image.jpg");
- }
- }
- }
Решение задачи: «Ошибка "A generic error occurred in GDI+"»
textual
Листинг программы
- tempImage.SaveAsFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "full.png"), ImageFormat.Png);
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д