0

I have the following problem or question, I have this function

private void SavePic(Canvas canvas, string filename)
    {
        RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
         (int)canvas.Width, (int)canvas.Height,
         96d, 96d, PixelFormats.Pbgra32);
        // needed otherwise the image output is black
        canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
        canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));

        renderBitmap.Render(canvas);

        //JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(renderBitmap));

        using (FileStream file = File.Create(filename))
        {

            encoder.Save(file);
        }
    }

and the corresponding call SavePic(mySuperDefaultPainting, @"C:\KinDraw\out.png");

Now I wanted to attach the file name the date + time? You can grab this DateTime function in the function call?

maybe I can someone help here?

Avishek
  • 1,896
  • 14
  • 33
viv1d
  • 339
  • 4
  • 5
  • 12
  • These will help: http://msdn.microsoft.com/en-us//library/system.datetime.now.aspx http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx http://msdn.microsoft.com/en-us/library/az4se3k1.aspx http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx – Andrew Savinykh Jan 29 '13 at 08:36

5 Answers5

3

try (Updated for File Path):

string fileName=string.Format("{0}-{1:ddMMMyyyy-HHmm}.png", @"C:\KinDraw\out", 
                                                    DateTime.Now);
if(!Directory.Exists(Path.GetDirectoryName(fileName)))
{
    Directory.CreateDirectory(Path.GetDirectoryName(fileName));
}

SavePic(mySuperDefaultPainting, fileName);

Say the time is 29-JAN-2013 07:30 PM it will give you: C:\KinDraw\out-29JAN2013-1930.png.

But please check details about CreateDirectory on this MSDN page. Also look for Exceptions and wrap in try-catch blocks.

TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
  • Thanks for all the tips and advice, but he always complains and crashes if the directory for the output not exisitert? Gibts there a way to create the directory if it does not exist? – viv1d Jan 29 '13 at 12:01
2
string timestamp =DateTime.Now.ToString("MMddyyyy.HHmmss");
SavePic(mySuperDefaultPainting, @"C:\KinDraw\out"+timestamp+".png");

Update: (to create the directory if it does not exist)

if (!Directory.Exists(filepath))
    Directory.CreateDirectory(filepath);

Hope it helps :)

Avishek
  • 1,896
  • 14
  • 33
  • Thanks for all the tips and advice, but he always complains and crashes if the directory for the output not exisitert? Gibts there a way to create the directory if it does not exist? – viv1d Jan 29 '13 at 12:01
  • @Avishek your updated statement will always return false when checking for directory and giving path that ends with file. – TheVillageIdiot Jan 29 '13 at 12:45
  • @viv1d : No, you have to assign only directory name(without file-name) to `filepath` – Avishek Jan 29 '13 at 13:16
1

Try to add this at the beginning of your code:

var extension = Path.GetExtension(filename);
var newName = filename.Replace(filename, extension) + DateTime.Now.ToString("yyyy-MM-dd HH:mm:dd") + extension;
nonacc
  • 21
  • 1
  • 6
  • string.Format() is your friend. – Malmi Jan 29 '13 at 08:38
  • Thanks for all the tips and advice, but he always complains and crashes if the directory for the output not exisitert? Gibts there a way to create the directory if it does not exist? – viv1d Jan 29 '13 at 12:01
1

Just put this line in there:

string stampedFileName = filename.Replace(".",
    string.Format("{0:YYYY-mm-dd hhmmss}", DateTime.UtcNow) + ".");

and then change

using (FileStream file = File.Create(filename))

to

using (FileStream file = File.Create(stampedFilename))

It is important to use DateTime.UtcNow rather than DateTime.Now because the former is not influenced by daylight saving time.

EDIT: The format I propose above has the advantage that sorting your filenames alphabetically then automatically also sorts them chronologically.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • Thanks for all the tips and advice, but he always complains and crashes if the directory for the output not exisitert? Gibts there a way to create the directory if it does not exist? – viv1d Jan 29 '13 at 12:02
  • if (!Directory.Exist(path)) { Directory.CreateDirectory(path); } – Roy Dictus Jan 29 '13 at 12:09
-1

This is how I did it and it works. Tweaked @Avishek code a bit to make it work for mine. No need to delete file or lose what's in it.

Call "Rename()" method after you output the file..

public static void Rename()
        {
            string timestamp = DateTime.Now.ToString("MMddyyyy.HHmmss"); 
            string originalFile = @"C:\Users\Data_Output\" + fileName + ".csv"; 
            string newFile = @"C:\Users\Data_Output\" + fileName + "_" + timestamp + ".csv";

            File.Move(originalFile, newFile);

        }
Jabir
  • 61
  • 7
  • This is a backwards solution. There's no need to first save the file with the wrong name and then rename it to the right name. Just save it with the right name the first time. – Joep Beusenberg Jul 21 '21 at 20:20