Nasıl yapılır: Çalışma Zamanında Resimleri Ayarlama (Windows Forms)

Windows Forms denetimi tarafından görünen görüntüyü programlı bir şekilde ayarlayabilirsiniz PictureBox .

Bir resmi programlı bir şekilde ayarlamak için

  • ImageSınıfının yöntemini kullanarak özelliği ayarlayın FromFileImage .

    Aşağıdaki örnekte, görüntü konumu için ayarlanan yol Belgelerim klasörüdür. bu, Windows işletim sistemini çalıştıran bilgisayarların çoğunun bu dizini içerdiğini varsaydığı için yapılır. Bu Ayrıca, en az sistem erişim düzeylerine sahip kullanıcıların uygulamayı güvenle çalıştırmasına olanak tanır. Aşağıdaki örnekte, bir PictureBox denetimin zaten eklendiği bir form varsayılır.

    Private Sub LoadNewPict()  
       ' You should replace the bold image
       ' in the sample below with an icon of your own choosing.  
       PictureBox1.Image = Image.FromFile _  
       (System.Environment.GetFolderPath _  
       (System.Environment.SpecialFolder.Personal) _  
       & "\Image.gif")  
    End Sub  
    
    private void LoadNewPict(){  
       // You should replace the bold image
       // in the sample below with an icon of your own choosing.  
       // Note the escape character used (@) when specifying the path.  
       pictureBox1.Image = Image.FromFile  
       (System.Environment.GetFolderPath  
       (System.Environment.SpecialFolder.Personal)  
       + @"\Image.gif");  
    }  
    
    private:  
       void LoadNewPict()  
       {  
          // You should replace the bold image
          // in the sample below with an icon of your own choosing.  
          pictureBox1->Image = Image::FromFile(String::Concat(  
             System::Environment::GetFolderPath(  
             System::Environment::SpecialFolder::Personal),  
             "\\Image.gif"));  
       }  
    

Grafiği temizlemek için

  • İlk olarak, görüntü tarafından kullanılan belleği serbest bırakın ve grafiği temizleyin. Bellek yönetimi bir sorun haline gelirse çöp toplama belleği daha sonra boşaltacaktır.

    If Not (PictureBox1.Image Is Nothing) Then  
       PictureBox1.Image.Dispose()  
       PictureBox1.Image = Nothing  
    End If  
    
    if (pictureBox1.Image != null)
    {  
       pictureBox1.Image.Dispose();  
       pictureBox1.Image = null;  
    }  
    
    if (pictureBox1->Image != nullptr)  
    {  
       pictureBox1->Image->Dispose();  
       pictureBox1->Image = nullptr;  
    }  
    

    Not

    Yöntemi bu şekilde nasıl kullanmanız gerektiği hakkında daha fazla bilgi için Dispose bkz. Dispose.

    Bu kod, tasarım zamanında denetime bir grafik yüklense bile görüntüyü temizler.

Ayrıca bkz.