簡體   English   中英

如何使用鼠標事件在圖片框中移動圖像

[英]How to move an image in picturebox using mouse events

我正在嘗試在圖片框中移動圖像。 我在應用程序中添加了面板,還在面板中添加了圖片框。 我打開了一個圖像。如果圖像尺寸很大。我想查看圖像的特定部分。 因此,如何上下移動圖像(不使用滾動條)以查看圖像的特定部分?

您可以添加控件,例如向左移動,向右移動,向上移動,向下移動以及相關操作,以在圖片框內移動圖像。 下面顯示了如何執行此操作以將圖像向右移動的示例。 您可以通過按下鼠標和按下鼠標事件來實現這些操作,以便用戶只需按適當的按鈕即可根據需要移動圖片。 還要注意,一旦達到圖像的最大尺寸,就可以將矩形區域更改為圖像范圍內的矩形區域。

int ff = 0; //number of positions to move
Bitmap b2;
private void button1_Click(object sender, EventArgs e)
{
    if (ff == 0) { b2 = new Bitmap(pictureBox1.Image);}  //original image as bitmap b2
    Bitmap b1 = new Bitmap(pictureBox1 .Width ,pictureBox1.Height );  //new bitmap with rectangular region of original image
    Rectangle r1 = new Rectangle(ff++, 0, pictureBox1.Width, pictureBox1.Height );
    Graphics g = Graphics.FromImage(b1);
    g.DrawImage(b2, 0, 0, r1, GraphicsUnit.Pixel);
    g.Dispose();
    pictureBox1.Image = null;
    pictureBox1.Image = (Image)b1;
    pictureBox1.Refresh();
}

不知道它是否真的回答了您的問題,但這似乎是一個使用Reactive Extensions(Rx)的有趣原因。 該視頻很好地展示了這些東西與異步事件(例如鼠標輸入)的配合效果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM