/// <summary>Saves the HTML contained in control to a string and return it.</summary> /// <returns>string - The HTML in the control</returns> public string SaveHtml() { if (!this.IsCreated) { throw new Exception("HtmlControl.SaveHtml : No HTML to save!"); } string content = String.Empty; try { NativeMethods.IHTMLDocument2 document = this.site.Document; // First save the document to a stream NativeMethods.IPersistStreamInit psi = (NativeMethods.IPersistStreamInit)document; Debug.Assert(psi != null, "Expected IPersistStreamInit"); NativeMethods.IStream stream = null; NativeMethods.CreateStreamOnHGlobal(NativeMethods.NullIntPtr, true, out stream); psi.Save(stream, 1); // Now copy the stream to the string NativeMethods.STATSTG stat = new NativeMethods.STATSTG(); stream.Stat(stat, 1); int length = (int)stat.cbSize; byte[] bytes = new byte[length]; IntPtr hglobal; NativeMethods.GetHGlobalFromStream(stream, out hglobal); Debug.Assert(hglobal != NativeMethods.NullIntPtr, "Failed in GetHGlobalFromStream"); // First copy the stream to a byte array IntPtr pointer = NativeMethods.GlobalLock(hglobal); if (pointer != NativeMethods.NullIntPtr) { Marshal.Copy(pointer, bytes, 0, length); NativeMethods.GlobalUnlock(hglobal); // Then create the string from the byte array (use a StreamReader to eat the preamble in the UTF8 encoding case) StreamReader streamReader = null; try { streamReader = new StreamReader(new MemoryStream(bytes), Encoding.Default); content = streamReader.ReadToEnd(); } finally { if (streamReader != null) { streamReader.Close(); } } } } catch (Exception exception) { Debug.Fail("HtmlControl.SaveHtml" + exception.ToString()); content = String.Empty; } finally { } if (content == null) { content = String.Empty; } return(content); /* * HtmlFormatter formatter = new HtmlFormatter(); * StringWriter writer = new StringWriter(); * formatter.Format(content, writer); * return writer.ToString(); */ }
/// <summary>Saves the HTML contained in control to a string and return it.</summary> /// <returns>string - The HTML in the control</returns> public string SaveHtml() { if (!this.IsCreated) { throw new Exception("HtmlControl.SaveHtml : No HTML to save!"); } string content = String.Empty; try { NativeMethods.IHTMLDocument2 document = this.site.Document; // First save the document to a stream NativeMethods.IPersistStreamInit psi = (NativeMethods.IPersistStreamInit)document; Debug.Assert(psi != null, "Expected IPersistStreamInit"); NativeMethods.IStream stream = null; NativeMethods.CreateStreamOnHGlobal(NativeMethods.NullIntPtr, true, out stream); psi.Save(stream, 1); // Now copy the stream to the string NativeMethods.STATSTG stat = new NativeMethods.STATSTG(); stream.Stat(stat, 1); int length = (int)stat.cbSize; byte[] bytes = new byte[length]; IntPtr hglobal; NativeMethods.GetHGlobalFromStream(stream, out hglobal); Debug.Assert(hglobal != NativeMethods.NullIntPtr, "Failed in GetHGlobalFromStream"); // First copy the stream to a byte array IntPtr pointer = NativeMethods.GlobalLock(hglobal); if (pointer != NativeMethods.NullIntPtr) { Marshal.Copy(pointer, bytes, 0, length); NativeMethods.GlobalUnlock(hglobal); // Then create the string from the byte array (use a StreamReader to eat the preamble in the UTF8 encoding case) StreamReader streamReader = null; try { streamReader = new StreamReader(new MemoryStream(bytes), Encoding.Default); content = streamReader.ReadToEnd(); } finally { if (streamReader != null) { streamReader.Close(); } } } } catch (Exception exception) { Debug.Fail("HtmlControl.SaveHtml" + exception.ToString()); content = String.Empty; } finally { } if (content == null) { content = String.Empty; } return content; /* HtmlFormatter formatter = new HtmlFormatter(); StringWriter writer = new StringWriter(); formatter.Format(content, writer); return writer.ToString(); */ }
private Object GetDataFromOleIStream(string format, DVASPECT aspect, int index) { FORMATETC formatetc; STGMEDIUM medium; formatetc = new FORMATETC(); formatetc.cfFormat = (short)DataFormats.GetDataFormat(format).Id; formatetc.dwAspect = aspect; formatetc.lindex = index; formatetc.tymed = TYMED.TYMED_ISTREAM; object outData = null; if (NativeMethods.S_OK == QueryGetDataInner(ref formatetc)) { GetDataInner(ref formatetc, out medium); try { // Check both handle and type of storage medium if (medium.unionmember != IntPtr.Zero && medium.tymed == TYMED.TYMED_ISTREAM) { UnsafeNativeMethods.IStream pStream; pStream = (UnsafeNativeMethods.IStream)Marshal.GetObjectForIUnknown(medium.unionmember); NativeMethods.STATSTG sstg = new NativeMethods.STATSTG(); pStream.Stat(sstg, NativeMethods.STATFLAG_DEFAULT); int size = (int)sstg.cbSize; IntPtr hglobal = Win32GlobalAlloc(NativeMethods.GMEM_MOVEABLE | NativeMethods.GMEM_DDESHARE | NativeMethods.GMEM_ZEROINIT, (IntPtr)(size)); try { IntPtr ptr = Win32GlobalLock(new HandleRef(this, hglobal)); try { // NTRAID:WINDOWS#1449332-2006/01/16-WAYNEZEN, // Seek to the beginning of the stream before reading it. pStream.Seek(0, 0 /* STREAM_SEEK_SET */); pStream.Read(ptr, size); } finally { Win32GlobalUnlock(new HandleRef(this, hglobal)); } outData = GetDataFromHGLOBAL(format, hglobal); } finally { Win32GlobalFree(new HandleRef(this, hglobal)); } } } finally { UnsafeNativeMethods.ReleaseStgMedium(ref medium); } } return outData; }