كيفية القيام بما يلي: تعيين الصور في وقت تشغيل (Windows Forms)

يمكنك برمجياً تعيين الصورة المعروضة بواسطة عنصر تحكم PictureBox الخاص بنماذج Windows.

لتعيين الصور برمجياً

  • قم بتعيين خاصية Image باستخدام أسلوب FromFile من فئة Image .

    في المثال أدناه ، المسار المحدد لموقع الصورة هو مجلد المستندات. ويتم ذلك لأنه بإمكانك افتراض أن معظم أجهزة الكمبيوتر التي تستخدم نظام التشغيل Windows تشمل هذا الدليل. كما يتيح هذا لمستخدمين مستويات وصول الحد الأدنى للنظام من تشغيل التطبيق بأمان. المثال التالي يفترض وجود نموذج مع عنصر تحكم PictureBox تمت إضافة مسبقاً.

    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.get_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"));
       }
    

لمسح الرسم

  • أولا، قم بتحرير الذاكرة المستخدمة بواسطة الصورة ثم قم بمسح الرسم. سيتم تحرير ذاكرة تجميع البيانات المهملة لاحقاً إذا أصبحت إدارة الذاكرة مشكلة.

    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;
    }
    

    ملاحظة

    للحصول على المزيد من المعلومات حول سبب وجوب استخدام أسلوب Dispose بهذه الطريقة، قم بمراجعة التنظيف موارد غير المُدارة.

    هذا الرمز سيقوم بمسح الصورة حتى إذا تم تحميل الرسم في المسيطر أثناء وقت التصميم.

راجع أيضًا:

المهام

كيفية: تحميل صورة باستخدام مصمم (نماذج Windows)

كيفية القيام بما يلي: تعديل الحجم أو الموضع من صورة في تشغيل الوقت (Windows Forms)

المرجع

مربع صورة عنصر تحكم نظرة عامة (Windows Forms)

PictureBox

Image.FromFile

موارد أخرى

عنصر تحكم مربع صورة (Windows Forms)