You must Sign In to post a response.
  • Category: Windows

    How to upload an image in windows application?


    Are you looking for information on uploading an image? Want to know how to upload a image in windows application? Read this thread to learn more about upload an image functionality with examples.



    I am creating a windows application where I need to upload image and I have to store them in a project folder. How to achieve it?
  • #720161
    Hi.
    Here is an example you can use to upload and save the image in a folder in the application:

    Aspx file:

    <asp:FileUpload ID="flUpload" runat="server" />
    <asp:Button ID="btnUpload" runat="server" text="Upload" onclick="btnUpload_Click"
    <asp:Image ID="img1" runat="server" />


    Code behind:

    protected void btnUpload_Click(object sender, EventArgs e)
    {
    string strImg = flUpload.FileName;
    string strImgPath = "ImageFolder/" + strImg ;

    if (flUpload.PostedFile != null && flUpload.PostedFile.FileName != "")
    {
    flUpload.SaveAs(Server.MapPath(strImgPath ));
    img1.ImageUrl = "~/" + strImgPath ;
    }
    }

    Thanks

    Reena

  • #720170
    To upload image in windows application you have to use Open file dialog box to select a file to upload and then just you have to copy the file to destination
    check following code snippet for more detail


    OpenFileDialog openDLG = new OpenFileDialog();
    openDLG.Multiselect = true;
    openDLG.ShowDialog();
    //put filter to file open dialog box to select image file only
    openDLG.Filter = "ImageFiles|*.Jpg|*.Gif|*.bmp|*.png";
    System.IO.File.Copy(openDLG.FileName , "D:\\" + Sustem.IO.Path.GetFileName(openDLG.FileName));
    MessageBox.Show("File upload successfully");


    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #720188
    please try something like below


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    OpenFileDialog1.ShowDialog()
    End Sub


    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    Dim _filename As String
    Dim _destination As String = "D:/UploadedImages"

    _filename = OpenFileDialog1.FileName
    If Not String.IsNullOrEmpty(_filename) Then
    File.Copy(_filename, _destination)
    End If
    End Sub

    Thanks!
    Regards,
    Narendra

  • #720288
    Hello

    Check this out.You need to use namespace using System.IO; and include meaning drag and drop the openFileDialog1 control




    private void button1_Click(object sender, EventArgs e)
    {
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK) // Test result.
    {
    textBox1.Text = openFileDialog1.FileName;
    File.Copy("source", "Server.MapPath(strImgPath )-destiantion");
    }
    }


    Hope this helps you.

  • #726488
    Hi Smitha Sestu,

    At first, load all required image file to a list box by using OpenFileDialog with below C# code:
    private void btnLoad_Click(object sender, EventArgs e)
    {
    OpenFileDialog ofdImgFusion = new OpenFileDialog();
    ofdImgFusion.Multiselect = true;
    ofdImgFusion.Filter = "Image File(*.JPG;*.PNG;*.BMP)|*.JPG;*.PNG;*.BMP";
    ofdImgFusion.Title = "Select a Image File";

    if (ofdImgFusion.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    lstSourceImg.Items.AddRange(ofdImgFusion.FileNames);
    }
    }

    Now select a image on listbox and click on save button to save to specific in path.
    private void btnSave_Click(object sender, EventArgs e)
    {
    StreamWriter swFusionResult = new StreamWriter(D:\Test\ProImg.jpg", false);
    pcbFusionImage.Image.Save(swFusionResult.BaseStream, ImageFormat.Jpeg);
    swFusionResult.Flush();
    swFusionResult.Close();
    }
    private void lstSourceImg_Click(object sender, EventArgs e)
    {
    Bitmap SourceImage = new Bitmap(lstSourceImg.SelectedItem.ToString());
    pcbSourceImage.Image = SourceImage;
    }


    Hope this example will be helpfull for you.
    For details please visit this link: http://www.dotnetspider.com/resources/44966-How-fusion-logo-or-banner-source-image.aspx

    Best regards
    Bashar


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.