Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > C# winApp เราจะดึงชื่อรูปภาพ มาจาก Properties.Resources ได้อย่างไร ครับ



 

C# winApp เราจะดึงชื่อรูปภาพ มาจาก Properties.Resources ได้อย่างไร ครับ

 



Topic : 137008



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook



คือผมลองสร้าง คลาส เกี่ยวกับรูปภาพมาแบบนี้ครับ

Code (C#)
   public partial class PanelPicture : UserControl
    {
        public PanelPicture()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog res = new OpenFileDialog();
            res.Filter = "Image Files|*.jpg;*.jpeg;*.png;";
            if (res.ShowDialog() == DialogResult.OK)
            {
                var filePath = res.FileName;
                textBox1.Text = filePath;
                pictureBox1.Image = Image.FromFile(filePath);
                OnPictureChanged(EventArgs.Empty);
            }
        }

        #region _Properties
        [Localizable(true)]
        public Image Image
        {
            [ResourceExposure(ResourceScope.Machine)]
            [ResourceConsumption(ResourceScope.Machine)]
            get
            {
                return pictureBox1.Image;
            }
            set
            {
                pictureBox1.Image = value;
                OnPictureChanged(EventArgs.Empty);
               
                if (value != null)
                 {
                     textBox1.Text = value.ToString();
                 }
                Invalidate();
            }
        }

        [
        DefaultValue(PictureBoxSizeMode.Normal),
        Localizable(true),
        RefreshProperties(RefreshProperties.Repaint)
        ]
        public PictureBoxSizeMode SizeMode
        {
            get
            {
                return pictureBox1.SizeMode;
            }
            set
            {
                pictureBox1.SizeMode = value;
            }
        }
        #endregion

        #region _Event
        public event EventHandler PictureChanged;
        protected virtual void OnPictureChanged(EventArgs e)
        {
            EventHandler handler = PictureChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        #endregion
    }


ฟหดหเ

ปัญหาติดตรงนี้ครับ

6653

ตอนเลือกรูปภาพจากหน้า ดีไซน์ มาแล้ว มันจะขึ้นเป็น System.Drawing.Bitmap ใน textBox1 ครับ

สว้



Tag : .NET, Win (Windows App), C#







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2023-05-03 16:00:28 By : lamaka.tor View : 236 Reply : 5
 

 
Code (C#)
private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog res = new OpenFileDialog();
    res.Filter = "Image Files|*.jpg;*.jpeg;*.png;";
    if (res.ShowDialog() == DialogResult.OK)
    {
        var filePath = res.FileName;
        var fileName = Path.GetFileName(filePath); // get only the file name from the full path
        textBox1.Text = fileName; // display the file name in the textbox
        pictureBox1.Image = Image.FromFile(filePath);
        OnPictureChanged(EventArgs.Empty);
    }
}







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-05-03 20:27:46 By : 009
 


 

No. 2



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ผมจะติดตรง Image Image เวลาเลือกจาก ภาพ มาจาก Properties.Resources จากหน้า ดีไซน์ มาแล้ว มันจะขึ้นเป็น System.Drawing.Bitmap ครับ

ตอนนี้ รันให้เป็นค่าว่างไปก่อน เพื่อให้ได้ใช้งานได้ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-05-04 07:55:43 By : lamaka.tor
 

 
ตอบความคิดเห็นที่ : 2 เขียนโดย : lamaka.tor เมื่อวันที่ 2023-05-04 07:55:43
รายละเอียดของการตอบ ::
ลองแก้ Image property

Code (C#)
[Localizable(true)]
public Image Image
{
    [ResourceExposure(ResourceScope.Machine)]
    [ResourceConsumption(ResourceScope.Machine)]
    get
    {
        return pictureBox1.Image;
    }
    set
    {
        pictureBox1.Image = value;
        OnPictureChanged(EventArgs.Empty);
        
        if (value != null)
        {
            var fileName = Path.GetFileName(value.ToString()); // get only the file name from the full path
            textBox1.Text = fileName; // display the file name in the textbox
        }
        else
        {
            textBox1.Text = ""; // clear the textbox if the image is null
        }
        Invalidate();
    }
}



แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-05-04 09:24:44 By : 009
 


 

No. 4



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 3 เขียนโดย : 009 เมื่อวันที่ 2023-05-04 09:24:44
รายละเอียดของการตอบ ::
ใช้เป็น Path.GetFileName ไม่ได้ครับ
เนื่องจาก value.ToString() จะมีค่าเป็น System.Drawing.Bitmap ครับ

สว้


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-05-04 09:35:34 By : lamaka.tor
 


 
มันเก็บเฉพาะ object ไม่ติด info มาด้วย
ลองสร้าง Info เอง แล้วดึงไปใช้ร่วมกัน ประมาณนี้

Code (C#)
public partial class PanelPicture : UserControl
{
    public PanelPicture()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog res = new OpenFileDialog();
        res.Filter = "Image Files|*.jpg;*.jpeg;*.png;";
        if (res.ShowDialog() == DialogResult.OK)
        {
            var filePath = res.FileName;
            var image = Image.FromFile(filePath);
            var imageInfo = new ImageInfo(image, filePath);
            textBox1.Text = filePath;
            pictureBox1.Image = image;
            Image = imageInfo; // set the Image property to the ImageInfo object
            OnPictureChanged(EventArgs.Empty);
        }
    }

    #region _Properties
    [Localizable(true)]
    public ImageInfo Image
    {
        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        get
        {
            return pictureBox1.Tag as ImageInfo; // get the ImageInfo object from the PictureBox's Tag property
        }
        set
        {
            pictureBox1.Image = value?.Image;
            pictureBox1.Tag = value; // store the ImageInfo object in the PictureBox's Tag property
            OnPictureChanged(EventArgs.Empty);

            if (value != null)
            {
                textBox1.Text = value.FilePath;
            }
            else
            {
                textBox1.Text = "";
            }
            Invalidate();
        }
    }

    [
    DefaultValue(PictureBoxSizeMode.Normal),
    Localizable(true),
    RefreshProperties(RefreshProperties.Repaint)
    ]
    public PictureBoxSizeMode SizeMode
    {
        get
        {
            return pictureBox1.SizeMode;
        }
        set
        {
            pictureBox1.SizeMode = value;
        }
    }
    #endregion

    #region _Event
    public event EventHandler PictureChanged;
    protected virtual void OnPictureChanged(EventArgs e)
    {
        EventHandler handler = PictureChanged;
        if (handler != null)
        {
            handler(this, e);
        }
    }
    #endregion
}

public class ImageInfo
{
    public Image Image { get; }
    public string FilePath { get; }

    public ImageInfo(Image image, string filePath)
    {
        Image = image;
        FilePath = filePath;
    }
}



ลองประยุกต์ ไม่สำเร็จอาจไม่ support คงต้องหาวิธีใหม่
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-05-04 10:29:51 By : 009
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : C# winApp เราจะดึงชื่อรูปภาพ มาจาก Properties.Resources ได้อย่างไร ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่