2013-04-14 11 views
6

से संलग्न करें क्या कोई जानता है कि सी # का उपयोग करके स्क्रीनशॉट कैसे लेना है और किसी एप्लिकेशन के किसी विशिष्ट कंटेनर/हिस्से की तस्वीर लेने के लिए इसे सीमित करना है। मैं पूरी स्क्रीन या एप्लिकेशन की पूरी खिड़की नहीं चाहता हूं।सी # आवेदन के भीतर .NET नियंत्रण का स्क्रीनशॉट लें और Outlook ईमेल

मेरा पैनल बस कहा जाता है: पैनल 1 उपयोगकर्ता "बटन" पर क्लिक करेगा और पैनल 1 का स्क्रीनशॉट लें और ईमेल से संलग्न होगा।

मैं केवल उस अनुभाग का स्क्रीन शॉट लेना चाहता हूं और फिर स्थानीय रूप से सी: \ ड्राइव और/या संलग्नक ईमेल में संलग्न या एम्बेड करना चाहता हूं।

मैंने इंटरनेट पर अन्य विभिन्न चीजें पढ़ीं लेकिन उनमें से अधिकतर को एक वेब ब्राउज़र नियंत्रण का एक स्क्रीनशॉट लेने में जटिल परिवर्तन करने के साथ निपटना पड़ा, जिसे मैं नहीं ढूंढ रहा हूं।

+0

संभव डुप्लिकेट की [सी #: कैसे स्क्रीन के एक हिस्से के एक स्क्रीनशॉट लेने के लिए] (http://stackoverflow.com/questions/3306600/c- कैसे-टू-टेक-ए-स्क्रीनशॉट-ऑफ-ऑफ-स्क्रीन-स्क्रीन) – MoonKnight

उत्तर

5

मैं यह कुछ की तरह

public static void TakeCroppedScreenShot(
    string fileName, int x, int y, int width, int height, ImageFormat format) 
{ 
    Rectangle r = new Rectangle(x, y, width, height); 
    Bitmap bmp = new Bitmap(r.Width, r.Height, PixelFormat.Format32bppArgb); 
    Graphics g = Graphics.FromImage(bmp); 
    g.CopyFromScreen(r.Left, r.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy); 
    bmp.Save(fileName, format); 
} 

का उपयोग कर मुझे आशा है कि इस मदद करता है

+0

वास्तव में, इसके साथ पहले से ही एक प्रश्न है: http://stackoverflow.com/questions/3306600/c-how-to-take -ए-स्क्रीनशॉट-ऑफ-ए-ऑफ-स्क्रीन – MoonKnight

4

तुम सिर्फ Panel's स्क्रीनशॉट चाहते हैं, तो आप उपयोग कर सकते हैं में निर्मित DrawToBitmap विधि।

Bitmap bmp = new Bitmap(myPanel.Width, myPanel.Height); 
myPanel.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height)); 
bmp.Save(@"C:\MyPanelImage.bmp"); 

बस ध्यान दें कि कुछ नियंत्रण ऐसे WebBrowser और RichTextBox नियंत्रण के रूप में इस कार्यक्षमता के साथ काम कर सकते हैं नहीं है लेकिन यह सबसे अन्य नियंत्रण के लिए काम करना चाहिए (पाठ बॉक्स, लेबल आदि ..)

+0

धन्यवाद, ये 2 लाइनें जो मैं लापता था उसकी कुंजी थी। बिटमैप बीएमपी = नया बिटमैप (myPanel.Width, myPanel.Height); myPanel.DrawToBitmap (बीएमपी, नया आयताकार (0, 0, बीएमपी। विथथ, बीएमपी.हेइट)); – brink668

+0

यह स्क्रीनशॉट बनाने के सवाल के पहले भाग का उत्तर देता है। ** फिर ** दृष्टिकोण और सामान। लेकिन मैं वैसे भी पहले भाग के लिए यहाँ था। – Bitterblue

1

मैं इस पाया है बिटमैप के रूप में एक नियंत्रण को बचाने के लिए:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    private void Button1_Click(object sender, EventArgs e) 
    { 
     SaveAsBitmap(panel1,"C:\\path\\to\\your\\outputfile.bmp"); 
    } 

    public void SaveAsBitmap(Control control, string fileName) 
    { 
     //get the instance of the graphics from the control 
     Graphics g = control.CreateGraphics(); 

     //new bitmap object to save the image 
     Bitmap bmp = new Bitmap(control.Width, control.Height); 

     //Drawing control to the bitmap 
     control.DrawToBitmap(bmp, new Rectangle(0, 0, control.Width, control.Height)); 

     bmp.Save(fileName); 
     bmp.Dispose(); 

    } 
} 

मैं दृष्टिकोण here के बारे में ऐसा कुछ मिला है, लेकिन मैं इसे परीक्षण नहीं कर सकता है, क्योंकि मैं दृष्टिकोण मेरे पीसी पर स्थापित नहीं है।

2

Killercam's answer का थोड़ा सा संशोधन:

public static Bitmap takeComponentScreenShot(Control control) 
    { 
     // find absolute position of the control in the screen. 
     Control ctrl = control; 
     Rectangle rect = new Rectangle(Point.Empty, ctrl.Size); 
     do 
     { 
      rect.Offset(ctrl.Location); 
      ctrl = ctrl.Parent; 
     } 
     while (ctrl != null); 

     Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); 
     Graphics g = Graphics.FromImage(bmp); 

     g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy); 

     return bmp; 
    } 

इस विधि स्क्रीनशॉट तुरंत ले जाएगा। उदाहरण के लिए, यदि आप इस फ़ंक्शन को कॉल करने से पहले पैनल के भीतर किसी बटन की दृश्यता बदलते हैं, तो बिटमैप में बटन होगा। यदि यह वांछित नहीं है, तो keyboardP's answer का उपयोग करें।

2

Asif's जवाब देने के लिए एक सुधार:

public static Bitmap takeComponentScreenShot(Control control) 
{ 
    // find absolute position of the control in the screen. 
    Rectangle rect=control.RectangleToScreen(control.Bounds); 

    Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); 
    Graphics g = Graphics.FromImage(bmp); 

    g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy); 

    return bmp; 
} 
+0

यह कैसे सुधार है? – beresfordt

+0

आयत की गणना करने के लिए अन्य तर्क कुछ बार विफल रहता है –

+0

अन्य विफल क्यों होता है और यह काम क्यों करता है? इस तरह की चीजें उत्तर में शामिल करने के लिए उपयोगी हैं! – beresfordt

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