2012-06-14 10 views
8

मैं सिर्फ मेरी picturebox.image पर एक चयन रखना चाहते थे पर एक क्षेत्र का चुनाव कैसे करें लेकिन इस बस कुछ थोड़ा कष्टप्रद स्थिति से भी बदतर हो गई है। मैंने मुख्य तस्वीर बॉक्स पर एक और तस्वीर बॉक्स पर सोचा लेकिन यह मेरे लिए इतना आलसी काम लग रहा था। मुझे यह जानने की ज़रूरत है कि एक पिक्चरबॉक्स पर चयन क्षेत्र (जो आधा पारदर्शी नीला क्षेत्र वाला है) बनाने का कोई तरीका है या नहीं। जो कि माउस के साथ आकर्षित करने वाला है और इसे उस छवि को बदलना नहीं चाहिए जिस पर मैं काम कर रहा हूं।सी # में माउस के साथ एक PictureBox.Image

नमूना:

// Start Rectangle 
    // 
    private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
    { 
     // Determine the initial rectangle coordinates... 
     RectStartPoint = e.Location; 
     Invalidate(); 
    } 

    // Draw Rectangle 
    // 
    private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 
    { 
     if (e.Button != MouseButtons.Left) 
      return; 
     Point tempEndPoint = e.Location; 
     Rect = 
      new Rectangle(
       Math.Min(RectStartPoint.X, tempEndPoint.X), 
       Math.Min(RectStartPoint.Y, tempEndPoint.Y), 
       Math.Abs(RectStartPoint.X - tempEndPoint.X), 
       Math.Abs(RectStartPoint.Y - tempEndPoint.Y)); 
     Invalidate(Rect); 
    } 

    // Draw Area 
    // 
    private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
    { 
     // Draw the rectangle... 
     if (pictureBox1.Image != null) 
     { 
      Brush brush = new SolidBrush(Color.FromArgb(128, 72, 145, 220)); 
      e.Graphics.FillRectangle(brush, Rect); 
     } 
    } 
+0

तो क्या आप एक तस्वीर बॉक्स में एक छवि पर एक चयन बॉक्स बनाना चाहते हैं? क्या चयन बॉक्स एक पारदर्शी नीला वर्ग बनाने के लिए डेस्कटॉप पर क्लिक करके और खींचने जैसा ही कार्य करेगा? – 3aw5TZetdf

उत्तर

30

मैं अपने कोड का इस्तेमाल किया है, तो आप लगभग वहाँ थे। आपको आयताकार के बजाय चित्रबॉक्स 1 को अमान्य करने की आवश्यकता है। मैंने रेक्ट के लिए एक चेक भी जोड़ा, इसलिए इसे प्रारंभ नहीं किया गया है या इसका कोई आकार नहीं है।

एक अन्य महत्वपूर्ण परिवर्तन: मैं आयत केवल एक बार बनाया है और मैं उसके स्थान और आकार समायोजित। साफ करने के लिए कम कचरा!

संपादित

मैं आयत के लिए एक माउस को राइट-क्लिक हैंडलर गयी।

private Point RectStartPoint; 
private Rectangle Rect = new Rectangle(); 
private Brush selectionBrush = new SolidBrush(Color.FromArgb(128, 72, 145, 220)); 

// Start Rectangle 
// 
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
{ 
    // Determine the initial rectangle coordinates... 
    RectStartPoint = e.Location; 
    Invalidate(); 
} 

// Draw Rectangle 
// 
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 
{ 
    if (e.Button != MouseButtons.Left) 
     return; 
    Point tempEndPoint = e.Location; 
    Rect.Location = new Point(
     Math.Min(RectStartPoint.X, tempEndPoint.X), 
     Math.Min(RectStartPoint.Y, tempEndPoint.Y)); 
    Rect.Size = new Size(
     Math.Abs(RectStartPoint.X - tempEndPoint.X), 
     Math.Abs(RectStartPoint.Y - tempEndPoint.Y)); 
    pictureBox1.Invalidate(); 
} 

// Draw Area 
// 
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
{ 
    // Draw the rectangle... 
    if (pictureBox1.Image != null) 
    { 
     if (Rect != null && Rect.Width > 0 && Rect.Height > 0) 
     { 
      e.Graphics.FillRectangle(selectionBrush, Rect); 
     } 
    } 
} 

private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Right) 
    { 
     if (Rect.Contains(e.Location)) 
     { 
      Debug.WriteLine("Right click"); 
     } 
    } 
} 
+0

क्या आप मुझे बता सकते हैं कि मैं उस चयन पर राइट क्लिक इवेंट कैसे बना सकता हूं? –

+1

बीटीडब्ल्यू: आपके कार्यान्वयन में ब्रश बार-बार बनाया जाता है। इसे रोकने की कोशिश करो। मैं इसके लिए भी अपना कोड समायोजित करूंगा। –

+0

अब यह वास्तव में नरम लगता है और उपयोगी .. धन्यवाद –

संबंधित मुद्दे