C
C#β€’4mo ago
leo

βœ… A generic error occurred in GDI+.' -> Save Image Path

Hey, I been trying to making some face recognition using EMGU.CV, but I faces a problem which, when I'm trying to save the picture, which the cropped side (faces) so I could using that to face recognition in future, but when I'm trying to save it by binding to button1, it give me a problem -> System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+.' .
private int faceCounter = 0;
private void button1_Click(object sender, System.EventArgs e)
{

if (pictureBox2.Image != null)
{

string savePath = Application.StartupPath + "/TrainedFaces/face" + faceCounter + ".bmp";
pictureBox2.Image.Save(savePath, System.Drawing.Imaging.ImageFormat.Bmp);

faceCounter++;

MessageBox.Show("Cropped face saved successfully.");
}
else
{
MessageBox.Show("No face detected to save.");
}
}
private int faceCounter = 0;
private void button1_Click(object sender, System.EventArgs e)
{

if (pictureBox2.Image != null)
{

string savePath = Application.StartupPath + "/TrainedFaces/face" + faceCounter + ".bmp";
pictureBox2.Image.Save(savePath, System.Drawing.Imaging.ImageFormat.Bmp);

faceCounter++;

MessageBox.Show("Cropped face saved successfully.");
}
else
{
MessageBox.Show("No face detected to save.");
}
}
private async Task StreamVideo2()
{
var faceCasade = new CascadeClassifier("./detection/haarcascade_frontalface_default.xml");
var vc = new VideoCapture(0, Emgu.CV.VideoCapture.API.DShow);

var faces = faceCasade.DetectMultiScale(frameGray, 1.3, 5);
while (streamVideo)
{
....

....
pictureBox2.Image = grayCroppedFace.ToBitmap();
....
var img = frame.ToBitmap();
pictureBox1.Image = img;


.....
}
}
private async Task StreamVideo2()
{
var faceCasade = new CascadeClassifier("./detection/haarcascade_frontalface_default.xml");
var vc = new VideoCapture(0, Emgu.CV.VideoCapture.API.DShow);

var faces = faceCasade.DetectMultiScale(frameGray, 1.3, 5);
while (streamVideo)
{
....

....
pictureBox2.Image = grayCroppedFace.ToBitmap();
....
var img = frame.ToBitmap();
pictureBox1.Image = img;


.....
}
}
No description
4 Replies
leowest
leowestβ€’4mo ago
have u checked if the folder exists
string folder = Path.Combine(Application.StartupPath, "TrainedFaces");
string savePath = Path.Combine(folder, $"face{faceCounter}.bmp");
if (!Directory.Exists(folder))
{
MessageBox.Show("Folder not found");
return;
}
string folder = Path.Combine(Application.StartupPath, "TrainedFaces");
string savePath = Path.Combine(folder, $"face{faceCounter}.bmp");
if (!Directory.Exists(folder))
{
MessageBox.Show("Folder not found");
return;
}
also use Path.Combine instead of manually doing as its safer alternatively instead of saving from the picturebox, can u try saving from a new object
using var bitmapImage = new Bitmap(pictureBox2.Image);
bitmapImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Bmp);
using var bitmapImage = new Bitmap(pictureBox2.Image);
bitmapImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Bmp);
usually that happens when it can't find the path to save or when GDI is still in use so having a different object to save can work in that case
leo
leoβ€’4mo ago
Silly me! I spend my 20 minutes figure what's the problem, and the problem that, I was on different project folder, since the folder named similar, the only different its the dot at the end! and thank you for the advice and the answer, I would happly hear all the suggestion.
MODiX
MODiXβ€’4mo ago
Use the /close command to mark a forum thread as answered
leowest
leowestβ€’4mo ago
no worries πŸ˜‰
Want results from more Discord servers?
Add your server
More Posts
Navigation and Dialogs with MVVM and WPFI'm using `CommunityToolkit.MVVM` which doesn't include pre-made classes for navigation and dialog iAPI for searchingHello, I'am doing a application for selling product online. And you know, the search bar, I want to how to search .txt file with textboxes and dropdownlists and print in a table or label vs webformsIm trying to search data from a .txt file with textboxes and dropdownlists, then print the item i secant set enhanced mouse precisionOk i still don’t have the mouse thing working. I’m trying to set the enhanced mouse position option Create JSON objects from System.Text.JsonFor a while now I've been trying to get away from dependencies that I don't need anymore, including How to change the button output. Instead of questions it should show some more buttons.``` using System; using System.Linq; using NativeUiLib; //This ads the GUI contols. Minor abstractio.NET 8 error when using ASP.NET AspNetCore.Reporting.LocalReport`var result = localReport.Execute(AspNetCore.Reporting.RenderType.ExcelOpenXml, 1, param, "");` Errcan we send extra data in .net8 identity endpointsi need to send some extra data add the time of register. this register end point only allow email anHelp with Switching Views using MVVMhttps://github.com/MasonOderSo/MVVMtesting I have no idea why this is not working it is working in Help me with my concurrency experimentsCorrect me if I'm wrong. Based on my understanding, a CPU with 1 core running 1 thread has the same