2010-11-01 14 views
28

में एक छवि लोड करें मैं एक छवि को PictureBox में लोड करना चाहता हूं। यह वह छवि है जिसे मैं लोड करना चाहता हूं: http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=identicon&r=PGएक यूआरएल से एक पिक्चरबॉक्स

मैं यह कैसे कर सकता हूं?

+0

और समस्या/सवाल क्या है? –

उत्तर

59

इनबिल्ट PictureBox.Load(string URL) Method "निर्दिष्ट URL पर ImageLocation सेट और छवि संकेत दिया प्रदर्शित करता है।" (चूंकि .NetFramework 2)

+1

यह बहुत आसान लगता है :)। –

+2

दुर्भाग्यवश, यदि प्रमाण-पत्र आवश्यक हैं तो मनुष्य को अन्य विधि से चिपकना होगा। – Larry

43

इस प्रयास करें:

var request = WebRequest.Create("http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=identicon&r=PG"); 

using (var response = request.GetResponse()) 
using (var stream = response.GetResponseStream()) 
{ 
    pictureBox1.Image = Bitmap.FromStream(stream); 
} 
+2

यह एक बहुत ही उपयोगी उदाहरण है। –

+1

यदि आप पृष्ठभूमि थ्रेड में छवि को डाउनलैड करना चाहते हैं तो यह एक बेहतर समाधान है। धन्यवाद! – YoniXw

6
yourPictureBox.ImageLocation = "http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=identicon&r=PG" 
4

यहां उपयोग किया गया समाधान है। मुझे याद नहीं है कि मैं सिर्फ पिक्चरबॉक्स का उपयोग नहीं कर सकता था। लोड विधियों। मुझे यकीन है कि ऐसा इसलिए है क्योंकि मैं पिक्चरबॉक्स नियंत्रण में डाउनलोड की गई छवि को & को उचित रूप से स्केल करना चाहता था। अगर मुझे याद है, तो पिक्चरबॉक्स पर सभी स्केलिंग विकल्प या तो छवि को फैलाते हैं, या छवि फिट करने के लिए पिक्चरबॉक्स का आकार बदल देंगे। मैं पिक्चरबॉक्स के लिए सेट आकार में एक उचित स्केल और केंद्रित छवि चाहता था।

अब, मैं सिर्फ एक async संस्करण बनाने की जरूरत है ...

यहाँ मेरी तरीके:

#region Image Utilities 

    /// <summary> 
    /// Loads an image from a URL into a Bitmap object. 
    /// Currently as written if there is an error during downloading of the image, no exception is thrown. 
    /// </summary> 
    /// <param name="url"></param> 
    /// <returns></returns> 
    public static Bitmap LoadPicture(string url) 
    { 
     System.Net.HttpWebRequest wreq; 
     System.Net.HttpWebResponse wresp; 
     Stream mystream; 
     Bitmap bmp; 

     bmp = null; 
     mystream = null; 
     wresp = null; 
     try 
     { 
      wreq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url); 
      wreq.AllowWriteStreamBuffering = true; 

      wresp = (System.Net.HttpWebResponse)wreq.GetResponse(); 

      if ((mystream = wresp.GetResponseStream()) != null) 
       bmp = new Bitmap(mystream); 
     } 
     catch 
     { 
      // Do nothing... 
     } 
     finally 
     { 
      if (mystream != null) 
       mystream.Close(); 

      if (wresp != null) 
       wresp.Close(); 
     } 

     return (bmp); 
    } 

    /// <summary> 
    /// Takes in an image, scales it maintaining the proper aspect ratio of the image such it fits in the PictureBox's canvas size and loads the image into picture box. 
    /// Has an optional param to center the image in the picture box if it's smaller then canvas size. 
    /// </summary> 
    /// <param name="image">The Image you want to load, see LoadPicture</param> 
    /// <param name="canvas">The canvas you want the picture to load into</param> 
    /// <param name="centerImage"></param> 
    /// <returns></returns> 

    public static Image ResizeImage(Image image, PictureBox canvas, bool centerImage) 
    { 
     if (image == null || canvas == null) 
     { 
      return null; 
     } 

     int canvasWidth = canvas.Size.Width; 
     int canvasHeight = canvas.Size.Height; 
     int originalWidth = image.Size.Width; 
     int originalHeight = image.Size.Height; 

     System.Drawing.Image thumbnail = 
      new Bitmap(canvasWidth, canvasHeight); // changed parm names 
     System.Drawing.Graphics graphic = 
        System.Drawing.Graphics.FromImage(thumbnail); 

     graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     graphic.SmoothingMode = SmoothingMode.HighQuality; 
     graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; 
     graphic.CompositingQuality = CompositingQuality.HighQuality; 

     /* ------------------ new code --------------- */ 

     // Figure out the ratio 
     double ratioX = (double)canvasWidth/(double)originalWidth; 
     double ratioY = (double)canvasHeight/(double)originalHeight; 
     double ratio = ratioX < ratioY ? ratioX : ratioY; // use whichever multiplier is smaller 

     // now we can get the new height and width 
     int newHeight = Convert.ToInt32(originalHeight * ratio); 
     int newWidth = Convert.ToInt32(originalWidth * ratio); 

     // Now calculate the X,Y position of the upper-left corner 
     // (one of these will always be zero) 
     int posX = Convert.ToInt32((canvasWidth - (image.Width * ratio))/2); 
     int posY = Convert.ToInt32((canvasHeight - (image.Height * ratio))/2); 

     if (!centerImage) 
     { 
      posX = 0; 
      posY = 0; 
     } 
     graphic.Clear(Color.White); // white padding 
     graphic.DrawImage(image, posX, posY, newWidth, newHeight); 

     /* ------------- end new code ---------------- */ 

     System.Drawing.Imaging.ImageCodecInfo[] info = 
         ImageCodecInfo.GetImageEncoders(); 
     EncoderParameters encoderParameters; 
     encoderParameters = new EncoderParameters(1); 
     encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 
         100L); 

     Stream s = new System.IO.MemoryStream(); 
     thumbnail.Save(s, info[1], 
          encoderParameters); 

     return Image.FromStream(s); 
    } 

    #endregion 

यहाँ आवश्यक भी शामिल है। (कुछ अन्य कोड द्वारा आवश्यक हो सकता है, लेकिन सहित सभी सुरक्षित रहने के लिए)

using System.Windows.Forms; 
using System.Drawing.Drawing2D; 
using System.IO; 
using System.Drawing.Imaging; 
using System.Text.RegularExpressions; 
using System.Drawing; 

कैसे मैं आम तौर पर इसका इस्तेमाल करते हैं:

ImageUtil.ResizeImage(ImageUtil.LoadPicture("http://someurl/img.jpg", pictureBox1, true); 
+1

आपके पास बहुत सारे संसाधन हैं जिनके बारे में आप सही तरीके से निपटान नहीं कर रहे हैं, और यह एक उपयोगिता विधि है, इसलिए संभवतः इसे अक्सर कहा जाता है: आपको उचित (var ... = 'या' डिसस्पेक्ट() 'कॉल का उपयोग करके उचित' जोड़ना चाहिए - System.Drawing.Image, System.Drawing.Graphics, बिटमैप, और स्ट्रीम सभी लागू करने योग्य IDISposable क्योंकि वे अप्रबंधित संसाधनों को संभालते हैं, इसलिए यदि आप उन्हें स्पष्ट रूप से रिलीज़ नहीं करते हैं तो यह बाद में आपको काटने के लिए वापस आ सकता है। – stuartd

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