Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy image from resources to disk

Tags:

c#

winforms

wpf

I added image to my solution. Changed compilation action property to resource and don't copy. Application in first run check is directories and files are existing if not they are created. My application needs default image of object. So thats why I added image to solution. Now how I can copy it to specific disk location.
I written this from samples which I founded.

if (!File.Exists(path_exe + "\\images\\drinks\\defaultIMG.jpg"))
{
    using (var resource =  Assembly.GetExecutingAssembly().GetManifestResourceStream("Data\\defaultIMG.jpg"))
    {
         using (var file = new FileStream(path_exe + "\\images\\drinks\\defaultIMG.jpg", FileMode.Create, FileAccess.Write))
         {
               resource.CopyTo(file);
         }
}

}

But this creates only empty file.

like image 286
GarryMoveOut Avatar asked Jul 26 '14 11:07

GarryMoveOut


2 Answers

I Think the easiest way is:

 Properties.Resources.defaultIMG.Save(path_exe + "\\images\\drinks\\defaultIMG.jpg");

Where defaultIMG is your Image resource name.

like image 93
Microlang Avatar answered Sep 19 '22 15:09

Microlang


I found sample of code on http://www.c-sharpcorner.com/uploadfile/40e97e/saving-an-embedded-file-in-C-Sharp/ It works good

Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Zeszycik.Data.defaultIMG.jpg");
FileStream fileStream = new FileStream("new.jpg", FileMode.CreateNew);
for (int i = 0; i < stream.Length; i++)
     fileStream.WriteByte((byte)stream.ReadByte());
fileStream.Close();
like image 43
GarryMoveOut Avatar answered Sep 19 '22 15:09

GarryMoveOut