我正在開發一個程式,該程式在圖片框中顯示來自尼康相機的 liveView 影像。我希望能夠將游標懸停在影像上并在另一個圖片框中顯示游標周圍的放大區域。我還想添加一個十字準線而不是滑鼠指標。到目前為止,我發現的唯一解決方案如下:
在游標后的第二個圖片框中縮放影像
它完全符合我的要求,但是我無法讓它作業。更具體地說,picZoom 中沒有顯示任何內容。在示例中,加載了影像,而在我的示例中,顯示了視頻流。這可能是我沒有讓它作業的原因。我對 c# 比較陌生,并沒有完全理解這個例子。
假設我有接收視頻流的 picBox。如何在 picZoom 中顯示游標周圍的一部分 picBox(假設尺寸為 x,y 的游標周圍的矩形),如示例中所示。我不需要擔心 picBox 和 picZoom 的不同尺寸,因為它們不會變化。我還希望能夠通過一個因子 zoomFactor 來改變縮放程度。
如果有人能給我指點或解決方案,將不勝感激。另外,很抱歉,如果我的問題格式不正確,我是論壇的新手。
謝謝!
亞歷山大
uj5u.com熱心網友回復:
我會這樣處理
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MagnifierExample
{
class Magnifier : PictureBox
{
public Magnifier()
{
Visible = false;
}
PictureBox source;
private Point sourcePoint;
Cursor oldCursor;
public PictureBox Source
{
get
{
return source;
}
set
{
if (source != null)
{
source.MouseEnter -= Source_MouseEnter;
source.MouseLeave -= Source_MouseLeave;
source.MouseMove -= Source_MouseMove;
source.Cursor = oldCursor;
}
source = value;
if (source != null)
{
source.MouseEnter = Source_MouseEnter;
source.MouseLeave = Source_MouseLeave;
source.MouseMove = Source_MouseMove;
oldCursor = source.Cursor;
source.Cursor = Cursors.Cross;
}
}
}
private void Source_MouseEnter(object sender, EventArgs e)
{
Visible = true;
BringToFront();
}
private void Source_MouseLeave(object sender, EventArgs e)
{
Visible = false;
}
private void Source_MouseMove(object sender, MouseEventArgs e)
{
sourcePoint = e.Location;
Location = new Point(source.Location.X e.X - Width / 2, source.Location.Y e.Y - Height / 2);
Invalidate();
}
protected override void WndProc(ref Message m)
{
const int WM_NCHITTEST = 0x0084;
const int HTTRANSPARENT = (-1);
if (!DesignMode && m.Msg == WM_NCHITTEST)
{
m.Result = (IntPtr)HTTRANSPARENT;
}
else
{
base.WndProc(ref m);
}
}
protected override void OnPaint(PaintEventArgs pe)
{
if (!DesignMode && Source != null && Source.Image != null)
{
Rectangle destRect = new Rectangle(0, 0, Width, Height);
// IMPORTANT: This calculation will depend on the SizeMode of the source and the amount you want to zoom.
// This does 2x zoom and works with PictureBoxSizeMode.Normal:
Rectangle srcRect = new Rectangle(sourcePoint.X - Width / 4, sourcePoint.Y - Height / 4, Width / 2, Height / 2);
pe.Graphics.DrawImage(Source.Image, destRect, srcRect, GraphicsUnit.Pixel);
}
else
{
base.OnPaint(pe);
}
}
}
}
要連接它,您只需Magnifier在表單中添加一個,給它一個大小,然后將其設定Source為PictureBox您希望它放大的大小。
this.magnifier1.Source = this.pictureBox1;
我在這個答案中使用了這種方法來忽略來自放大鏡視窗的訊息。
重要的是,我只撰寫了PictureBoxSizeMode.Normal源 PictureBox 的 SizeMode 的縮放數學。但我認為這是一個很好的開始。
Not 100% sure what you wanted for crosshairs, but this uses the built-in crosshair cursor.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/437728.html
