2010-12-10 10 views

Antwort

5

können Sie so etwas wie dieses

private void SaveImage() 
    { 
     try 
     { 

      SaveFileDialog saveDialog = new SaveFileDialog(); 
      saveDialog.Filter = "JPeg Image(*.JPG)|*.jpg|Bitmap Image(*.BMP)|*.bmp|Png Image(*.PNG)|*.png|Gif Image(*.GIF)|*.gif"; 

      if (saveDialog.ShowDialog().Value == true) 
      { 
       // Save current canvas transform 
       Transform transform = img.LayoutTransform; 
       // reset current transform (in case it is scaled or rotated) 
       img.LayoutTransform = null; 

       // Get the size of canvas 
       Size size = new Size(img.ActualWidth, img.ActualHeight); 
       // Measure and arrange the surface 
       // VERY IMPORTANT 
       img.Measure(size); 
       img.Arrange(new Rect(size)); 

       // Create a render bitmap and push the surface to it 
       RenderTargetBitmap renderBitmap = 
        new RenderTargetBitmap(
        (int)size.Width, 
        (int)size.Height, 
        96d, 
        96d, 
        PixelFormats.Default); 
       renderBitmap.Render(img); 
       BitmapEncoder encoder = new BmpBitmapEncoder(); 
       string extension = saveDialog.FileName.Substring(saveDialog.FileName.LastIndexOf('.')); 
       switch (extension.ToLower()) 
       { 
        case ".jpg": 
         encoder = new JpegBitmapEncoder(); 
         break; 
        case ".bmp": 
         encoder = new BmpBitmapEncoder(); 
         break; 
        case ".gif": 
         encoder = new GifBitmapEncoder(); 
         break; 
        case ".png": 
         encoder = new PngBitmapEncoder(); 
         break; 
       } 
       // push the rendered bitmap to it 
       encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); 
       // Create a file stream for saving image 
       using (FileStream fs = File.Open(saveDialog.FileName, FileMode.OpenOrCreate)) 
       { 
        encoder.Save(fs); 
       } 
       // Restore previously saved layout 
       img.LayoutTransform = transform; 
      } 

     } 
     catch (Exception) 
     { 

      throw; 
     } 
    } 
tun