ASP.NET - how to display image from fileupload controll to editor tool

Asked By mani on 24-Jan-12 01:19 AM
hi.. i have custom editor tool and i am customizing image upload option in that editor..
but after uploaded image in fileupload controll  how to display in editor tool in c# coding..???.
smr replied to mani on 24-Jan-12 01:31 AM
hi

follow this

Use openfiledialog on OpenToolStripMenuItem_Click event:

 

     private void OpenToolStripMenuItem_Click(object sender, EventArgs e)

      {

        OpenFileDialog Dlg = new OpenFileDialog();

        Dlg.Filter = "";

        Dlg.Title = "Select image";

        if (Dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)

        {

          Img = Image.FromFile(Dlg.FileName);

          //Image.FromFile(String) method creates an image from the specifed file, here dlg.Filename contains the name of the file from which to create the image

          LoadImage();

        }

 

      }

 

private void LoadImage()

      {

        //we set the picturebox size according to image, we can get image width and height with the help of Image.Width and Image.height properties.

        int imgWidth = Img.Width;

        int imghieght = Img.Height;

        PictureBox1.Width = imgWidth;

        PictureBox1.Height = imghieght;

        PictureBox1.Image = Img;

        PictureBoxLocation();

        OriginalImageSize = new Size(imgWidth, imghieght);

 

        SetResizeInfo();

      }

After doing this the image will always show in the center of the panel.

 

    private void PictureBoxLocation()

      {

        int _x = 0;

        int _y = 0;

        if (SplitContainer1.Panel1.Width > PictureBox1.Width)

        {

          _x = (SplitContainer1.Panel1.Width - PictureBox1.Width) / 2;

        }

        if (SplitContainer1.Panel1.Height > PictureBox1.Height)

        {

          _y = (SplitContainer1.Panel1.Height - PictureBox1.Height) / 2;

        }

        PictureBox1.Location = new Point(_x, _y);

      }

 

private void SplitContainer1_Panel1_Resize(object sender, EventArgs e)

      {

        PictureBoxLocation();

      }

  

  
follow
http://xtractpro.com/articles/Image-Editor.aspx?page=2
http://www.c-sharpcorner.com/uploadfile/hirendra_singh/how-to-make-image-editor-tool-in-C-Sharp/
http://www.authorcode.com/making-image-editing-tool-in-c/


dipa ahuja replied to mani on 24-Jan-12 02:37 AM
try this code. This will save your file in the images folder and if the image file already exist then it wil rename it.
 
<asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload>
<asp:Button ID="UploadButton" Text="Upload file" OnClick="UploadButton_Click" runat="server"/>
 
 
protected void UploadButton1_Click(object sender, EventArgs e)
{
  if (FileUpload1.HasFile)
  {
    // Call a helper method routine to save the file.
    // Specify the path to save the uploaded file to.
    string savePath = Server.MapPath("images/");
 
    // Get the name of the file to upload.
    string fileName = FileUpload1.FileName;
 
    // Create the path and file name to check for duplicates.
    string pathToCheck = savePath + fileName;
 
    // Create a temporary file name to use for checking duplicates.
    string tempfileName = "";
 
    // Check to see if a file already exists with the, same name as the file to upload.      
    if (File.Exists(pathToCheck))
    {
      int counter = 2;
      while (File.Exists(pathToCheck))
      {
        // if a file with this name already exists,
        // prefix the filename with a number.
        tempfileName = counter.ToString() + fileName;
        pathToCheck = savePath + tempfileName;
        counter++;
      }
 
      fileName = tempfileName;
 
      // Notify the user that the file name was changed.
      UploadStatusLabel.Text = "A file with the same name already exists." +
        "<br />Your file was saved as " + fileName;
    }
    else
    {
      // Notify the user that the file was saved successfully.
      UploadStatusLabel.Text = "Your file was uploaded successfully.";
    }
 
    // Append the name of the file to upload to the path.
    savePath += fileName;
    FileUpload1.SaveAs(savePath);
     
    Image1.ImageUrl = "images/" + FileUpload1.FileName;
  }
  else
  {
    // Notify the user that a file was not uploaded.
    UploadStatusLabel.Text = "You did not specify a file to upload.";
  }
}