serdar

Code Snippet

public class program

{

static bool complete = false;

[STAThread]

static void Main()

{

WebBrowser wb = new WebBrowser();

wb.Width = 576;

wb.Height = 864;

wb.ScrollBarsEnabled = false;

wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);

wb.Navigate(@"C:\sample.htm");

while (!complete)

{

Thread.Sleep(50);

Application.DoEvents();

}

ConvertToMetafile2(0, wb);

ConvertToMetafile2(1, wb);

}

static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

{

complete = true;

}

static void ConvertToMetafile2(int i, WebBrowser wb)

{

Metafile mf;

FileStream ms = new FileStream("c:\\" + i.ToString() + ".emf", FileMode.Create);

try

{

IntPtr hDCMem = CreateCompatibleDC((IntPtr)null);

Rectangle bounds = new Rectangle(0, 0, 576, 864);

mf = new Metafile(ms, hDCMem, bounds,

MetafileFrameUnit.Pixel, EmfType.EmfOnly);

Graphics graphics = Graphics.FromImage(mf);

IntPtr memdc = graphics.GetHdc();

OleDraw(wb.ActiveXInstance, 1, memdc, ref bounds);

graphics.ReleaseHdc(memdc);

graphics.Dispose();

DeleteDC(hDCMem);

}

catch (Exception ex)

{

Debug.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);

throw;

}

finally

{

ms.Close();

}

}

[DllImport("Gdi32.dll")]

public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

[DllImport("ole32.dll", ExactSpelling = true, SetLastError = true)]

public static extern int OleDraw

([MarshalAs(UnmanagedType.IUnknown)] object pUnkn,

int dwAspect, IntPtr hDC, ref Rectangle R);

[DllImport("Gdi32.dll")]

public static extern bool DeleteDC(IntPtr hdc);

}

Hi,

What can i do for each returned emf files is equal to each other I mean without re-navigate. This code generate two emf file and first file is ok but second contains only bitmap.

Thanks for all...



Re: Windows Forms General Metafiles from WebBrowser

nobugz

The documentation for OleDraw() specifically mentions you shouldn't draw into a metafile.





Re: Windows Forms General Metafiles from WebBrowser

serdar

Code Snippet

public class program

{

static bool complete = false;

[STAThread]

static void Main()

{

Form form = new Form();

WebBrowser wb = new WebBrowser();

wb.Parent = form;

WebBrowser wb = new WebBrowser();

wb.Width = 576;

wb.Height = 864;

wb.ScrollBarsEnabled = false;

wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);

wb.Navigate(@"C:\sample.htm");

while (!complete)

{

Thread.Sleep(50);

Application.DoEvents();

}

form.ShowDialog();

ConvertToMetafile2(0, wb);

ConvertToMetafile2(1, wb);

}

static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

{

complete = true;

}

static void ConvertToMetafile2(int i, WebBrowser wb)

{

Metafile mf;

FileStream ms = new FileStream("c:\\" + i.ToString() + ".emf", FileMode.Create);

try

{

IntPtr hDCMem = CreateCompatibleDC((IntPtr)null);

Rectangle bounds = new Rectangle(0, 0, 576, 864);

mf = new Metafile(ms, hDCMem, bounds,

MetafileFrameUnit.Pixel, EmfType.EmfOnly);

Graphics graphics = Graphics.FromImage(mf);

IntPtr memdc = graphics.GetHdc();

OleDraw(wb.ActiveXInstance, 1, memdc, ref bounds);

graphics.ReleaseHdc(memdc);

graphics.Dispose();

DeleteDC(hDCMem);

}

catch (Exception ex)

{

Debug.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);

throw;

}

finally

{

ms.Close();

}

}

[DllImport("Gdi32.dll")]

public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

[DllImport("ole32.dll", ExactSpelling = true, SetLastError = true)]

public static extern int OleDraw

([MarshalAs(UnmanagedType.IUnknown)] object pUnkn,

int dwAspect, IntPtr hDC, ref Rectangle R);

[DllImport("Gdi32.dll")]

public static extern bool DeleteDC(IntPtr hdc);

}

Okay, I changed few line in the code. Now its work perfectly. (It's only Form.ShowDialog methot) How can explain this I don't want show any form this case.





Re: Windows Forms General Metafiles from WebBrowser

nobugz

You already knew this from your previous thread, right





Re: Windows Forms General Metafiles from WebBrowser

serdar

Yes, it's very weird. I don't understand why ShowDialog. If i find a solution will be great.



Re: Windows Forms General Metafiles from WebBrowser

nobugz

Check this thread.





Re: Windows Forms General Metafiles from WebBrowser

serdar

Thanks. But i found another threat http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=870255&SiteID=1 that fits in this case. But i already wonder ShowDialog's magic Smile