【问题标题】:How to get the path of picturebox image in resources and transfer it to a label如何获取资源中picturebox图片的路径,并传递给标签
【发布时间】:2019-06-29 22:14:54
【问题描述】:

我有一个表单,用户可以注册必填字段,并且他们可以选择浏览桌面上的图像。我没有将实际图像保存在我的数据库中。我使用了 Openfiledialog,当用户选择图像时,它会将路径复制到标签,所选图像将保存在我的项目文件夹中,路径将保存在我的数据库中。现在的问题是如果用户不想有图片怎么办?为了解决这个问题,我使用资源以编程方式将图像设置到我的图片框。现在的问题是如何获取图片框的图像路径(图像在资源中)并将其复制到我的标签,并且您还可以看到我有 File.Copy 功能我不希望在何时启用此代码用户没有选择任何图像。

PS:如果没有上传图片,默认图片如facebook

public partial class RegisterCustomer : UserControl
{
    public RegisterCustomer()
    {
        InitializeComponent();
        UserImage.Image = Properties.Resources.User;
        //var img = Image.FromFile(@"C:\Users\dieth\source\repos\SalesInventoryManagement\SalesInventoryManagement\bin\Debug\Icons\User.png");
        //UserImage.Image = img;
    }     
    Register rc = new Register();
    private void Btn_Register_Click(object sender, EventArgs e)
    {
        File.Copy(lbl_location.Text, Path.Combine(@"C:\Users\dieth\source\repos\SalesInventoryManagement\SalesInventoryManagement\bin\Picture", Path.GetFileName(lbl_location.Text)), true);
        rc.Insert(lbl_location.Text, string.Format(txt_firstname.Text + " " + txt_middlename.Text + " " + txt_lastname.Text), cbox_gender.Text, txt_contact.Text, txt_email.Text, txt_address.Text, Variables.Current_Date);
        MessageBox.Show("Successfully Added", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
    }
    private void Btn_Browse_Click(object sender, EventArgs e)
    {
        rc.BrowseImage(UserImage, Variables.location , lbl_location);      
    }
}

public class Register
{
    public void Insert(string path, string Fullname, string Gender, string Contact_Number, string Email, string Home_Address, string Dates)
    {       
        using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesInventoryManagement.Properties.Settings.Setting"].ConnectionString))
        {
            using (var cmd = new SqlCommand("usp_InsertCustomer", con))
            {
                con.Open();
                byte[] b = File.ReadAllBytes(path);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Images", SqlDbType.VarChar).Value = path;
                cmd.Parameters.Add("@Full_Name", SqlDbType.VarChar, 50).Value = Fullname;
                cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 50).Value = Gender;
                cmd.Parameters.Add("@Contact_Number", SqlDbType.VarChar, 11).Value = Contact_Number;
                cmd.Parameters.Add("@Email", SqlDbType.VarChar, 50).Value = Email;
                cmd.Parameters.Add("@Home_Address", SqlDbType.VarChar, 50).Value = Home_Address;
                cmd.Parameters.Add("@Dates", SqlDbType.Date).Value = Dates;
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
    public void BrowseImage(PictureBox UserImage, string location, Label path)
    {
        using (OpenFileDialog ofd = new OpenFileDialog())
        {
            ofd.Filter = "Image Files (*.jpg;*.jpeg;.*.png; | *.jpg;*.jpeg;.*.png;)";
            ofd.FilterIndex = 1;
            ofd.Multiselect = false;
            ofd.Title = "Select Image File";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                location = ofd.FileName;
                path.Text = location;
                UserImage.Image = Image.FromFile(location);
                UserImage.SizeMode = PictureBoxSizeMode.StretchImage;             
            }
        }
    }

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    您可以使用以下方法获取资源路径。

    var path = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,
                                              @"..\..\Resources","User.png"));
    

    【讨论】:

    • var path = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"C:\Users\dieth\source\repos\SalesInventoryManagement\SalesInventoryManagement\bin\Picture", "用户.png"));我修改了您的代码,因为我收到此错误“找不到此路径”,我的代码中的此路径是我的 User.png 所在的位置
    • 您将完整路径作为第二个参数传递。我认为您会想要对完整路径进行硬编码。您可能应该使用 "Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,@"..\Picture"))"
    猜你喜欢
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2021-04-24
    • 1970-01-01
    • 2020-12-09
    • 2016-01-06
    相关资源
    最近更新 更多