修改程式0118

This commit is contained in:
JEFF 2025-01-18 14:49:26 +08:00
parent 13f6021ffe
commit f170514531
3 changed files with 66 additions and 4 deletions

View File

@ -79,7 +79,48 @@ namespace Camera_connect
var camera = allCameras[0]; var camera = allCameras[0];
camera.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.Continuous); camera.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.Continuous);
camera.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber); camera.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
camera.StreamGrabber.ImageGrabbed += (sender, e) =>
{
var grabResult = e.GrabResult;
if (grabResult.IsValid)
{
Bitmap bmp = GrabResultToBitmap(grabResult);
UpdatePictureBox(bmp);
} }
};
}
// 將 IGrabResult 轉換為 Bitmap
private Bitmap GrabResultToBitmap(IGrabResult grabResult)
{
Bitmap bmp = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppArgb);
BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
pxConvert.OutputPixelFormat = PixelType.BGRA8packed;
pxConvert.Convert(bitmapData.Scan0, bitmapData.Stride * bmp.Height, grabResult);
bmp.UnlockBits(bitmapData);
return bmp;
}
// 更新 PictureBox 的顯示
private void UpdatePictureBox(Bitmap bmp)
{
form.Invoke((MethodInvoker)delegate
{
Bitmap resizedImage = new Bitmap(form.pictureBox1.Width, form.pictureBox1.Height);
using (Graphics g = Graphics.FromImage(resizedImage))
{
g.DrawImage(bmp, 0, 0, form.pictureBox1.Width, form.pictureBox1.Height);
}
Bitmap? oldImage = form.pictureBox1.Image as Bitmap;
form.pictureBox1.Image = resizedImage;
oldImage?.Dispose();
});
}
public void Stop() public void Stop()

View File

@ -172,7 +172,7 @@
private Button Bt_OneShot; private Button Bt_OneShot;
private Button Bt_KeepShot; private Button Bt_KeepShot;
private Button Bt_Stop; private Button Bt_Stop;
private PictureBox pictureBox1; public PictureBox pictureBox1;
private Label Label_status; private Label Label_status;
private ComboBox comboBox1; private ComboBox comboBox1;
private TextBox camera_serialnumber; private TextBox camera_serialnumber;

View File

@ -139,6 +139,7 @@ namespace Camera_connect
if (image != null) if (image != null)
{ {
pictureBox1.Image = image; pictureBox1.Image = image;
Label_status.Text = "IDS單張擷取";
} }
} }
} }
@ -148,6 +149,7 @@ namespace Camera_connect
if (Basler != null) if (Basler != null)
{ {
Basler.OneShot(); // 單次拍攝 Basler.OneShot(); // 單次拍攝
Label_status.Text = "Basler單張擷取";
} }
else else
{ {
@ -169,6 +171,7 @@ namespace Camera_connect
{ {
// IDS 相機的連續拍攝邏輯 // IDS 相機的連續拍攝邏輯
StartKeepShot(() => IDS_camera.GetPicture()); StartKeepShot(() => IDS_camera.GetPicture());
Label_status.Text = "IDS連續取像";
} }
else if (selectedCamera == "Basler") else if (selectedCamera == "Basler")
{ {
@ -177,6 +180,7 @@ namespace Camera_connect
{ {
Basler.KeepShot(); Basler.KeepShot();
isKeepShotting = true; isKeepShotting = true;
Label_status.Text = "Basler連續取像";
} }
else else
{ {
@ -255,16 +259,33 @@ namespace Camera_connect
{ {
pictureBox1.Invoke((MethodInvoker)delegate pictureBox1.Invoke((MethodInvoker)delegate
{ {
// 縮放影像以適應 PictureBox
Bitmap resizedImage = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(resizedImage))
{
g.DrawImage(bmp, 0, 0, pictureBox1.Width, pictureBox1.Height);
}
// 更新 PictureBox
Bitmap? old = pictureBox1.Image as Bitmap; Bitmap? old = pictureBox1.Image as Bitmap;
pictureBox1.Image = bmp; pictureBox1.Image = resizedImage;
old?.Dispose(); old?.Dispose();
}); });
} }
private void Bt_disconnect_Click(object sender, EventArgs e) private void Bt_disconnect_Click(object sender, EventArgs e)
{
string? selectedCamera = comboBox1.SelectedItem?.ToString();
if (selectedCamera == "IDS")
{
Label_status.Text = "IDS相機中斷連線";
}
else if(selectedCamera == "Basler")
{ {
Basler.Close(); Basler.Close();
Label_status.Text = "相機中斷連線"; Label_status.Text = "Basler相機中斷連線";
}
} }
} }
} }