Metafile export under windows 98 causesInvalidOperationExcep

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
CitoTChartUser
Newbie
Newbie
Posts: 8
Joined: Mon Mar 01, 2004 5:00 am

Metafile export under windows 98 causesInvalidOperationExcep

Post by CitoTChartUser » Wed May 25, 2005 8:36 am

Hello,

Code: Select all

Public Shared Function GetImage(ByVal chart As TChart) As Image 
  Dim strm As New IO.MemoryStream 
    With chart.Export.Image.Metafile 
    .Enhanced = True 
    .Width = chart.Width 
    .Height = chart.Height 
    .Save(strm) 
    strm.Position = 0 
  End With 
  Dim img As Image = Image.FromStream(strm) 
  strm.Flush() 
  Return img 
End Function 

Framework version: 1.1.4322.573 TeeChart version: 1.1.1879.21176
Under Windows 98 ONLY we get the following error when export a chart as MetaFile. Exporting the same chart as PNG or JPEG works just fine.

************** Exception Text **************
System.InvalidOperationException: The object is currently in use elsewhere.
at System.Drawing.Graphics.Dispose(Boolean disposing)
at System.Drawing.Graphics.Dispose()
at Steema.TeeChart.Export.MetafileFormat.SaveMetafile(Object FileOrStream)
at Steema.TeeChart.Export.MetafileFormat.Save(Stream stream)

We do not use dotted styles and are able to export to MetaFile using native Framework functions

Not being able to export to MetaFile under Windows 98 is a real showstopper for us.
Because about 60% of our custumers still works with Windows 98.

Would you please investigate this problem and let us now if this can be fixed. If so what's the timeschedule for this? If not are there alternatives to make MetaFile export workable under Windows 98? (ComInterop?)

Regards, Jan

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Wed May 25, 2005 10:59 am

Hello Jan,

You could try if something like the C# code below, which mostly relies on .NET native methods except for tChart1.Draw(g), works for you.

Code: Select all

		private void SaveChartToAMetafile(object FileOrStream) 
		{
			Bitmap bmp;
			Graphics g = null;
			System.Drawing.Imaging.Metafile mf = null;

			try 
			{
				bmp = new Bitmap(tChart1.Width,tChart1.Height,PixelFormat.Format32bppArgb);
				g = Graphics.FromImage(bmp);

				IntPtr hdc = g.GetHdc();

				if(FileOrStream is string)
					mf = new System.Drawing.Imaging.Metafile(FileOrStream as string,hdc,EmfType.EmfOnly);
				else if(FileOrStream is Stream)
					mf = new System.Drawing.Imaging.Metafile(FileOrStream as Stream,hdc,EmfType.EmfOnly);

				g.ReleaseHdc(hdc);
				g.Dispose();
				g = Graphics.FromImage(mf);
				tChart1.Draw(g);
				g.Dispose();
			}
			finally 
			{
				if(g != null) 
					mf.Dispose();
			}
		}
Converting it to VB, using http://www.kamalpatel.net/ConvertCSharp2VB.aspx, you get:

Code: Select all

		Private  Sub SaveChartToAMetafile(ByVal FileOrStream As Object)
			Dim bmp As Bitmap
			Dim g As Graphics =  Nothing 
			Dim mf As System.Drawing.Imaging.Metafile =  Nothing 
 
			Try
				bmp = New Bitmap(tChart1.Width,tChart1.Height,PixelFormat.Format32bppArgb)
				g = Graphics.FromImage(bmp)
 
				Dim hdc As IntPtr =  g.GetHdc() 
 
				If TypeOf FileOrStream Is String Then
					mf = New System.Drawing.Imaging.Metafile(FileOrStream as string,hdc,EmfType.EmfOnly)
				Else If TypeOf FileOrStream Is Stream Then 
					mf = New System.Drawing.Imaging.Metafile(FileOrStream as Stream,hdc,EmfType.EmfOnly)
				End If
 
				g.ReleaseHdc(hdc)
				g.Dispose()
				g = Graphics.FromImage(mf)
				tChart1.Draw(g)
				g.Dispose()
			Finally
				If Not g Is Nothing Then
					mf.Dispose()
				End If
			End Try
		End Sub
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

CitoTChartUser
Newbie
Newbie
Posts: 8
Joined: Mon Mar 01, 2004 5:00 am

Alternative does not work

Post by CitoTChartUser » Thu May 26, 2005 8:35 pm

The function 'SaveChartToAMetafile' works fine under w2k or wxp but under w98 we get the following error.

Code: Select all

System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
   at System.Drawing.Imaging.Metafile..ctor(Stream stream, IntPtr referenceHdc, EmfType type, String description)
   at System.Drawing.Imaging.Metafile..ctor(Stream stream, IntPtr referenceHdc, EmfType type)
   at TeeChartTest.Form1.SaveChartToAMetafile(Stream FileOrStream)

Regards, Jan

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Fri May 27, 2005 10:32 am

Hi Jan,

Then you could try not using the stream support option, just using:

Code: Select all

      Private  Sub SaveChartToAMetafile(ByVal FileOrStream As String) 
         Dim bmp As Bitmap 
         Dim g As Graphics =  Nothing 
         Dim mf As System.Drawing.Imaging.Metafile =  Nothing 

         Try 
            bmp = New Bitmap(tChart1.Width,tChart1.Height,PixelFormat.Format32bppArgb) 
            g = Graphics.FromImage(bmp) 

            Dim hdc As IntPtr =  g.GetHdc() 

            mf = New System.Drawing.Imaging.Metafile(FileOrStream as string,hdc,EmfType.EmfOnly) 

            g.ReleaseHdc(hdc) 
            g.Dispose() 
            g = Graphics.FromImage(mf) 
            tChart1.Draw(g) 
            g.Dispose() 
         Finally 
            If Not g Is Nothing Then 
               mf.Dispose() 
            End If 
         End Try 
      End Sub
If it doesn't work, as it seems being a .NET Framework with Win98 problem. you can try replacing line:

Code: Select all

            tChart1.Draw(g) 
for:

Code: Select all

				Dim blackPen As Pen =  New Pen(Color.Black,3) 
				g.DrawRectangle(blackPen ,0,0,200,100)
This way TeeChart is completly appart from this code. Once you get this code working in Win98 then place back TeeChart line and it should work.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

CitoTChartUser
Newbie
Newbie
Posts: 8
Joined: Mon Mar 01, 2004 5:00 am

Post by CitoTChartUser » Tue May 31, 2005 9:41 am

Hello Narcis,

Changing the following lines

Code: Select all

      'bmp = New Bitmap(chart.Width, chart.Height, PixelFormat.Format32bppArgb)
      'g = Graphics.FromImage(bmp)
into

Code: Select all

      g = chart.CreateGraphics
solves our problem, the reason remains unclear to me...


Regards, Jan

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Tue May 31, 2005 11:57 am

Hello Jan,
solves our problem, the reason remains unclear to me...
I'm glad to hear that. The problems lies on the .NET Framework, the workaround you found was already implemented on TeeChart source code but seems not to be working properly. However we are working on it and we are going to fix it for the next release.

Thanks for your collaboration.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply