2010-02-18 6 views
15

Comment sauvegarder des fichiers image (types jpg ou png) en C#?Enregistrer les fichiers image en C#

+4

Cette question est très vague, pouvez-vous fournir plus de détails sur ce que vous essayez d'accomplir? –

+3

Il voulait savoir comment enregistrer une image, c'est ce qu'il a obtenu. –

+4

salut, c'est vrai, mes moyens étaient ce que tu as dit. mais je suis elle :) –

Répondre

22

en C# nous la méthode Image.Save avec ces paramètres (chaîne Nom du fichier, ImageFormat)

http://msdn.microsoft.com/en-us/library/9t4syfhh.aspx

Est-ce tout ce que vous aviez besoin?

// Construct a bitmap from the button image resource. 
Bitmap bmp1 = new Bitmap(typeof(Button), "Button.bmp"); 

// Save the image as a GIF. 
bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Gif); 
18
Image bitmap = Image.FromFile("C:\\MyFile.bmp"); 
bitmap.Save("C:\\MyFile2.bmp"); 

Vous devriez être en mesure d'utiliser le Save Method du Image Class et être très bien comme indiqué ci-dessus. La méthode d'enregistrement a 5 options différentes ou ... surcharges

//Saves this Image to the specified file or stream. 
    img.Save(filePath); 

    //Saves this image to the specified stream in the specified format. 
    img.Save(Stream, ImageFormat); 

    //Saves this Image to the specified file in the specified format. 
    img.Save(String, ImageFormat); 

    //Saves this image to the specified stream, with the specified encoder and image encoder parameters. 
    img.Save(Stream, ImageCodecInfo, EncoderParameters); 

    //Saves this Image to the specified file, with the specified encoder and image-encoder parameters. 
    img.Save(String, ImageCodecInfo, EncoderParameters); 
0

Si vous avez besoin traitement des images plus étendue que le Net Framework fournit de la boîte, consultez le projet FreeImage

0
  SaveFileDialog sv = new SaveFileDialog(); 
      sv.Filter = "Images|*.jpg ; *.png ; *.bmp"; 
      ImageFormat format = ImageFormat.Jpeg; 

      if (sv.ShowDialog() == DialogResult.OK) 
      { 

       switch (sv.Filter) 
       { 
        case ".jpg": 

         format = ImageFormat.Png; 
         break; 

        case ".bmp": 

         format = ImageFormat.Bmp; 
         break; 
       } 


       pictureBox.Image.Save(sv.FileName, format); 
      } 
+0

Ajouter quelques explications avec réponse pour la façon dont cette réponse aide OP dans la fixation de problème actuel –

Questions connexes