how to copy and paste image using c#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmalsingh
    New Member
    • Sep 2006
    • 218

    how to copy and paste image using c#?

    hai all,
    i want to copy an image from a source(d:\\imag e.gif) and paste it in a folder(e:\\nirm al). how to do this using c#?
    i'll be more helpfull if anyone provide me a sample code.
    thanx in advance
    with Cheers
    Nirmal.
  • krille
    New Member
    • Feb 2008
    • 26

    #2
    Hey!

    This is one way to do it. I have used this when i needed to make some changes to the image if you just want to copy the image you could use StreamWriter to save the image insted.

    Stream buffer;
    StreamReader srImage = new StreamReader(st rPathNFileName) ;
    buffer = srImage.BaseStr eam;
    Bitmap bmImage = new Bitmap(buffer);
    bmImage.Save(st rNewPathNFileNa me)


    Good luck!
    /Krille

    Comment

    • nirmalsingh
      New Member
      • Sep 2006
      • 218

      #3
      Originally posted by krille
      Hey!

      This is one way to do it. I have used this when i needed to make some changes to the image if you just want to copy the image you could use StreamWriter to save the image insted.

      Stream buffer;
      StreamReader srImage = new StreamReader(st rPathNFileName) ;
      buffer = srImage.BaseStr eam;
      Bitmap bmImage = new Bitmap(buffer);
      bmImage.Save(st rNewPathNFileNa me)


      Good luck!
      /Krille
      i got a error " a generic error occured in GDI+".
      Code:
         myText.Text = fileDialog.FileName;
                          strPathNFileName=fileDialog.FileName;
                          Stream buffer;
                          StreamReader srImage = new StreamReader(strPathNFileName);
                          buffer = srImage.BaseStream;
                          Bitmap bmImage = new Bitmap(buffer);
                          StreamWriter srSettings = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Images\\");
                          srSettings.Write(bmImage);

      Comment

      • krille
        New Member
        • Feb 2008
        • 26

        #4
        Try not to use the StreamWriter insted use the code that i pasted, the method Save() in the Bitmap class

        Comment

        • nirmalsingh
          New Member
          • Sep 2006
          • 218

          #5
          Originally posted by krille
          Try not to use the StreamWriter insted use the code that i pasted, the method Save() in the Bitmap class
          thank you so much. it is working, but it is saving as system.drawing. bitmap


          Code:
          myText.Text = fileDialog.FileName;
                              strPathNFileName=fileDialog.FileName;
                              Stream buffer;
                              StreamReader srImage = new StreamReader(strPathNFileName);
                              buffer = srImage.BaseStream;
                              Bitmap bmImage = new Bitmap(buffer);
                              bmImage.Save(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Images\\"+bmImage);

          Comment

          • krille
            New Member
            • Feb 2008
            • 26

            #6
            Since you are using bmImage as file name and concat it to your path you will force it to call it´s ToString() method wich outputs what your file will be named.

            this is your code:
            Code:
            bmImage.Save(AppDomain.CurrentDomain.BaseDirectory   + "..\\..\\Images\\"+bmImage);
            use this insted:
            Code:
            string strFileName= fileDialog.FileName.Substring(fileDialog.FileName.LastIndexOf("\\") + 1);
            bmImage.Save(AppDomain.CurrentDomain.BaseDirectory   + "..\\..\\Images\\"+strFileName);
            this will give your file same name as it had before....

            Hope this helps
            /Krille
            Last edited by krille; Feb 4 '08, 04:02 PM. Reason: misstype

            Comment

            • wimpos
              New Member
              • Jan 2008
              • 19

              #7
              Why not simply using the File Class

              Code:
              File.Copy(oldPath, newPath);
              just 1 line of code that does it all: copy a file

              Comment

              • krille
                New Member
                • Feb 2008
                • 26

                #8
                So true. Go with that since you not gonna use any features of the Bitmap class anyway

                Comment

                • nirmalsingh
                  New Member
                  • Sep 2006
                  • 218

                  #9
                  Originally posted by krille
                  So true. Go with that since you not gonna use any features of the Bitmap class anyway
                  Thank you so much Krille and Wimpos, Both your Codes are working Fine.

                  Comment

                  Working...