C
C#10mo ago
Wubao

✅ 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
leowest10mo 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
Wubao
WubaoOP10mo 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
MODiX10mo ago
Use the /close command to mark a forum thread as answered
leowest
leowest10mo ago
no worries 😉
Want results from more Discord servers?
Add your server