 |
|
C# winApp เราจะดึงชื่อรูปภาพ มาจาก Properties.Resources ได้อย่างไร ครับ |
|
 |
|
|
 |
 |
|
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 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมจะติดตรง Image Image เวลาเลือกจาก ภาพ มาจาก Properties.Resources จากหน้า ดีไซน์ มาแล้ว มันจะขึ้นเป็น System.Drawing.Bitmap ครับ
ตอนนี้ รันให้เป็นค่าว่างไปก่อน เพื่อให้ได้ใช้งานได้ครับ
|
 |
 |
 |
 |
Date :
2023-05-04 07:55:43 |
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 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|