Click here to Skip to main content
15,120,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm working on creating a puzzle on wpf and I need some help in saving pieces of a picture in a specific folder to reload them if the user requested..
1. I checked if the directory exists
2. if not, I wrote:
if (!Directory.Exists(path1))

3. I need to save pieces into the previous folder! I got the Original Image from an open file dialog then I cut it into 100 pieces at run time

Can Anyone help me saving an image in a folder?

The Image class directly has a Save method you can use. See:[^]
r.nd.k 30-Dec-11 16:11pm
Thank you ! But isn't what you gave me for Windows form not wpf?
because I already tried it but (image1.Save) didn't exist!
Wendelius 30-Dec-11 16:31pm
Actually it's a GDI+ class. But if you're using WPF Image class directly then perhaps an easier way would be to encode it directly. Have a look at this article:[^]
NandaKumer 30-Dec-11 21:14pm
good link
Wendelius 31-Dec-11 3:39am
Thank you NandaKumer
r.nd.k 31-Dec-11 10:08am
The process cannot access the file because it is being used by another process.
I Always Get this error ! I know I have to dispose the Image but how?
I used this one!
the framework elememt can be a type Image right?

void SaveToPng(FrameworkElement visual, string fileName)
var encoder = new PngBitmapEncoder();
SaveUsingEncoder(visual, fileName, encoder);

void SaveUsingEncoder(FrameworkElement visual, string fileName, BitmapEncoder encoder)
RenderTargetBitmap bitmap = new RenderTargetBitmap(
BitmapFrame frame = BitmapFrame.Create(bitmap);

using (var stream = File.Create(fileName))

What I did was cutting the original image into pieces
each piece i memorized its data in a struct
struct ImagePuzzle
public System.Windows.Controls.Image pieces;
At the end I want to save each piece in a specific folder !
But I'm getting the previous error because I guess I have to dispose them !
How Can I do such thing?
to Encodes a collection of BitmapFrame objects to an image stream.
FileStream stream = new FileStream("empty.tif", FileMode.Create);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();

as you can use given function

public static void SaveClipboardImageToFile(string filePath)
    var image = Clipboard.GetImage();
    using (var fileStream = new FileStream(filePath, FileMode.Create))
        BitmapEncoder encoder = new PngBitmapEncoder();
r.nd.k 31-Dec-11 10:19am
Thnx but
var image = Clipboard.GetImage();
Always gives me null !
How can I fixe that?
RaviRanjanKr 31-Dec-11 10:38am
The object must be serializable for it to be put on the Clipboard. If you pass a non-serializable object to a Clipboard method, the method will fail without throwing an exception

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900