簡體   English   中英

將圖片加載到PictureBox中並繪制一個矩形

[英]Loading an image in the PictureBox and draw a rectangle

我寫了這段代碼:

private void ResizeImage()
{
    SetImage(Image);
}

private void SetPen()
{
    CreatePen(10,10,20,60);
}

private void CreatePen(int x, int y, int width, int height)
{
    Rectangle = new Rectangle(x, y, width, height);
    Pen = new Pen(Color.Crimson, 1);
    (Image.CreateGraphics()).DrawRectangle(Pen, Rectangle);
    Invalidate();
}

問題在於方法Load()將圖像替換為矩形。 我正在做圖像Cropper,用戶無法在其中創建新選擇。 該程序將自己創建選擇,用戶只能移動它。

在PictureBox的Paint事件中繪制矩形:

void PictureBox_Paint(object sender, PaintEventArgs e)
{
    Rectangle = new Rectangle(x, y, width, height);
    Pen = new Pen(Color.Crimson, 1);
    e.Graphics.DrawRectangle(Pen, Rectangle);
}

暫無
暫無

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

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