修改程式0118
This commit is contained in:
parent
13f6021ffe
commit
f170514531
@ -79,8 +79,49 @@ namespace Camera_connect
|
||||
var camera = allCameras[0];
|
||||
camera.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.Continuous);
|
||||
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()
|
||||
{
|
||||
|
2
Camera_connect/Main.Designer.cs
generated
2
Camera_connect/Main.Designer.cs
generated
@ -172,7 +172,7 @@
|
||||
private Button Bt_OneShot;
|
||||
private Button Bt_KeepShot;
|
||||
private Button Bt_Stop;
|
||||
private PictureBox pictureBox1;
|
||||
public PictureBox pictureBox1;
|
||||
private Label Label_status;
|
||||
private ComboBox comboBox1;
|
||||
private TextBox camera_serialnumber;
|
||||
|
@ -139,6 +139,7 @@ namespace Camera_connect
|
||||
if (image != null)
|
||||
{
|
||||
pictureBox1.Image = image;
|
||||
Label_status.Text = "IDS單張擷取";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -148,6 +149,7 @@ namespace Camera_connect
|
||||
if (Basler != null)
|
||||
{
|
||||
Basler.OneShot(); // 單次拍攝
|
||||
Label_status.Text = "Basler單張擷取";
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -169,6 +171,7 @@ namespace Camera_connect
|
||||
{
|
||||
// IDS 相機的連續拍攝邏輯
|
||||
StartKeepShot(() => IDS_camera.GetPicture());
|
||||
Label_status.Text = "IDS連續取像";
|
||||
}
|
||||
else if (selectedCamera == "Basler")
|
||||
{
|
||||
@ -177,6 +180,7 @@ namespace Camera_connect
|
||||
{
|
||||
Basler.KeepShot();
|
||||
isKeepShotting = true;
|
||||
Label_status.Text = "Basler連續取像";
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -255,16 +259,33 @@ namespace Camera_connect
|
||||
{
|
||||
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;
|
||||
pictureBox1.Image = bmp;
|
||||
pictureBox1.Image = resizedImage;
|
||||
old?.Dispose();
|
||||
});
|
||||
}
|
||||
|
||||
private void Bt_disconnect_Click(object sender, EventArgs e)
|
||||
{
|
||||
Basler.Close();
|
||||
Label_status.Text = "相機中斷連線";
|
||||
string? selectedCamera = comboBox1.SelectedItem?.ToString();
|
||||
if (selectedCamera == "IDS")
|
||||
{
|
||||
Label_status.Text = "IDS相機中斷連線";
|
||||
}
|
||||
else if(selectedCamera == "Basler")
|
||||
{
|
||||
Basler.Close();
|
||||
Label_status.Text = "Basler相機中斷連線";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user