Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Helloo!
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:
C#
if (!Directory.Exists(path1))
              {
                  Directory.CreateDirectory(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

Anyway
Can Anyone help me saving an image in a folder?
Posted

The Image class directly has a Save method you can use. See:http://msdn.microsoft.com/en-us/library/ktx83wah.aspx[^]
 
Share this answer
 
v2
Comments
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: http://blogs.msdn.com/b/kirillosenkov/archive/2009/10/12/saving-images-bmp-png-etc-in-wpf-silverlight.aspx[^]
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(
(int)visual.ActualWidth,
(int)visual.ActualHeight,
96,
96,
PixelFormats.Pbgra32);
bitmap.Render(visual);
BitmapFrame frame = BitmapFrame.Create(bitmap);
encoder.Frames.Add(frame);

using (var stream = File.Create(fileName))
{
encoder.Save(stream);
}
}



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?
Try
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapencoder.aspx[^]
to Encodes a collection of BitmapFrame objects to an image stream.
FileStream stream = new FileStream("empty.tif", FileMode.Create);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);


as you can use given function

C#
public static void SaveClipboardImageToFile(string filePath)
{
    var image = Clipboard.GetImage();
    using (var fileStream = new FileStream(filePath, FileMode.Create))
    {
        BitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        encoder.Save(fileStream);
    }
}
 
Share this answer
 
Comments
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