1

I am using Chart Control of .NET 4.0 framework in Windows Forms Application i have been saving Pie Chart image on location through PieChart.SaveImage(Path,ChartImageFormat.Png), when i create doc file with Microsoft.Office.Interop.Word i paste that image in that document. it proceed first time very well and .doc created successfully, but i try to save pie chart 2nd time during win forms Running it give an System.IO.Exception

"The process cannot access the file 'path' because it is being used by another process."

When i terminate program and run it again it over wright previous image but when i want to save image 2nd time during program running it gives same Exception

This is how i am saving image

private Void SavePieChart()
{
    string PieChartPath= Application.StartupPath + @"\Chart.png";
    PieChart.SaveImage(PieChartPath, ChartImageFormat.Png);
}

I searched, but did not find any efficient solution which solve my problem, If anything doing wrong Kindly Point out my mistake, or any helping link to solve this. .

EDIT 1

This is where I am pasting that image in Doc file

System.Drawing.Image PieChart =System.Drawing.Image.FromFile(PieChartPath);
oHeader1 = oDoc.Content.Paragraphs.Add(ref oMissing);
Logothread = new Thread(() => Clipboard.SetImage(PieChart));
Logothread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
Logothread.Start();
Logothread.Join();
oHeader1.Range.Paste();
oHeader1.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
oHeader1.Range.InsertParagraphAfter();

Thanks in advance

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Asad Kamal
  • 21
  • 7
  • 1
    Saving twice should work without a problem. Do you open it in c# as well? If not suspect that word is still holing on to the image? Does it work when you close word before saving it a 2nd time? Does it work without pasting into word? – TaW Aug 15 '16 at 11:14
  • Try to give all the saved png files a unique name. – LoekD Aug 15 '16 at 11:14
  • @TaW: I first save image and then paste it into the Doc file, at the end of creation i close the document and quit the application, it works fine. when it try to create doc file 2nd time after some time it shows exception – Asad Kamal Aug 15 '16 at 11:19
  • @LoekD: it can be done but it will fill the memory in the hard disk, that's why i am not doing that ... – Asad Kamal Aug 15 '16 at 11:20
  • I didn't catch the interop part, sorry. So you have the Image open in your app. This means you can't overwrite it unless you first __either__ manage to get rid of __all__ handles or __delete it from the disk__.. A very common issue, usually when changing an image and then trying to overwrite it. See [here](http://stackoverflow.com/questions/37736815/overwrite-image-picturebox-in-c-sharp/37741101?s=1|0.3231#37741101) for a post on how to avoid it in the first place by using a stream to read it, but there are dozens of other post with other advice as well – TaW Aug 15 '16 at 11:23
  • @Taw: no i dont have image Open in my app, any where, i update my Question please check it where i am pasting pie chart in doc file – Asad Kamal Aug 15 '16 at 11:27
  • does the winword.exe process keep running after you've created the word document? – LoekD Aug 15 '16 at 11:28
  • @LoekD: No there is no such process running after file is created – Asad Kamal Aug 15 '16 at 11:34
  • 2
    `Image.FromFile` keeps the file in use. You can use `Image.FromStream` instead. – Reza Aghaei Aug 15 '16 at 11:36
  • _System.Drawing.Image PieChart =System.Drawing.Image.FromFile(PieChartPath);_ Well here you __do__ open it. It is not as simple as one would wish to get rid of the handles the .NET system keeps from an FromFile call :-( – TaW Aug 15 '16 at 11:37
  • @RezaAghaei: Thanks it solved my problem – Asad Kamal Aug 15 '16 at 12:02
  • @TaW: thank you dear to pointing out my huge mistake ,, this solve my problem Thanks – Asad Kamal Aug 15 '16 at 12:03
  • Always glad to help, esp. with such a icky problem.. – TaW Aug 15 '16 at 12:05

1 Answers1

1

Problem is when i wast pasting image in doc file i was take image like

System.Drawing.Image PieChart =System.Drawing.Image.FromFile(PieChartPath);

as Reza and Taw described FromFile() keeps the file in use so that's why when i try to save image 2nd time it shows exception the file is already in process,

The i use FromStram() add this into my code

byte[] DataBytes= System.IO.File.ReadAllBytes(PieChartPath);
System.IO.MemoryStream ms = new System.IO.MemoryStream(DataBytes);

System.Drawing.Image PieChart = System.Drawing.Image.FromStream(ms);
Asad Kamal
  • 21
  • 7