int count = 0;
private void pictureBoxImageToCrop_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left || crop == false) return;
if (DrawingRects.Count > 0 && pictureBoxImageToCrop.Image != null && selectedPath != "")
{
if ((x >= 0 && x <= pictureBoxImageToCrop.Image.Size.Width) && (y >= 0 && y <= pictureBoxImageToCrop.Image.Size.Height))
{
var dr = DrawingRects.Last();
if (dr.Rect.Width > 0 && dr.Rect.Height > 0)
{
rectImage = cropAtRect((Bitmap)pictureBoxImageToCrop.Image, dr.Rect);
if (saveRectangles)
{
count++;
rectangleName = GetNextName(Path.Combine(selectedPath, "Rectangle"), ".bmp");
FileList.Add($"{dr.Location}, {dr.Size}", rectangleName);
string json = JsonConvert.SerializeObject(
FileList,
Formatting.Indented
);
using (StreamWriter sw = new StreamWriter(Path.Combine(selectedPath, "rectangles.txt"), false))
{
sw.WriteLine("Total number of rectangles: " + count + Environment.NewLine);
sw.Write(json);
sw.Close();
}
rectImage.Save(rectangleName);
saveRectanglesCounter++;
}
else
{
var stream = ToMemoryStream(rectImage);
var image = System.Drawing.Image.FromStream(stream);
pictureBoxCroppedImages.Image = image;
}
// Here I added an additional check to make sure the Image is set in pictureBoxCroppedImages
if (saveRectangles && pictureBoxCroppedImages.Image == null)
{
pictureBoxCroppedImages.Image = rectImage;
}
pixelsCounter = rect.Width * rect.Height;
pictureBoxCroppedImages.Invalidate();
var newKey = $"{dr.Location}, {dr.Size}";
var newItem = new MyListBoxItem
{
Message = newKey + "," + FileList[newKey],
ItemColor = Color.Green
};
MyItems.Add(newItem);
listBoxCroppedImages.SelectedIndex = MyItems.Count - 1;
pictureBoxImageToCrop.Focus();
// Check if the image is null before using it
if (pictureBoxCroppedImages.Image != null)
{
Graphics g = Graphics.FromImage(this.pictureBoxCroppedImages.Image);
g.Clear(this.pictureBoxCroppedImages.BackColor);
}
}
}
else
{
if (clearRectangles)
{
DrawingRects.Clear();
pictureBoxImageToCrop.Invalidate();
}
x = 0;
y = 0;
}
}
}Loading
Saravanan GanesanPosted Sep 24, 2023, 4:52 PM
When a C# WinForms rectangle drawn doesn't appear on the first attempt, it may be due to the drawing code not being triggered properly. Ensure that you're drawing within the Paint event handler of your control or form, as this event is responsible for rendering graphics. Verify that the code initializes and sets the proper properties for the Pen and Brush used for drawing. Additionally, call the control's
Invalidate()method to force a repaint when you want to update the graphics. Debug your code for any issues, and make sure the drawing logic is in the right place and properly executed within the Paint event handler.Sam HobbsPosted Sep 23, 2023, 6:57 PM
What is pictureBoxCroppedImages?
What do you do in the paint handler? My memory of Windows Forms is a bit fuzzy. Perhaps you need to do the actual drawing in the paint handler. If I knew what pictureBoxCroppedImages is then I could check on how it is used.
Daniel LipmanPosted Sep 21, 2023, 10:11 PM
in c# winforms, when drawing a rectangle then in the mouseup event first time it's not showing the cropped image in the pictureBoxCroppedImages it does show the next time i draw rectangles in the mouseup but the first time it's not showing. what could be the problem and how to solve?