public static void SaveObjectToStream(object obj, Stream stm) { IStreamImpl istm = new IStreamImpl(stm); IPersistStream ps = obj as IPersistStream; if (ps != null) { ps.Save(istm, false); } else { IPersistStreamInit psi = (IPersistStreamInit)obj; psi.Save(istm, false); } }
public string GetHTMLSource(HtmlDocument Document, Encoding Encode) { if (Document == null) { return(string.Empty); } //Declare vars int HRESULT; IStream pStream = null; IPersistStreamInit pPersistStreamInit = null; // Query for IPersistStreamInit. pPersistStreamInit = Document.DomDocument as IPersistStreamInit; if (pPersistStreamInit == null) { return(string.Empty); } //Create stream, delete on release HRESULT = CreateStreamOnHGlobal(IntPtr.Zero, true, out pStream); if ((pStream == null) || (HRESULT != S_OK)) { return(string.Empty); } //Save HRESULT = pPersistStreamInit.Save(pStream, false); if (HRESULT != S_OK) { return(string.Empty); } //Now read from stream.... //First get the size long ulSizeRequired = (long)0; ////LARGE_INTEGER //long liBeggining = (long)0; System.Runtime.InteropServices.ComTypes.STATSTG statstg = new System.Runtime.InteropServices.ComTypes.STATSTG(); pStream.Seek(0, (int)tagSTREAM_SEEK.STREAM_SEEK_SET, IntPtr.Zero); pStream.Stat(out statstg, (int)tagSTATFLAG.STATFLAG_NONAME); //Size ulSizeRequired = statstg.cbSize; if (ulSizeRequired == (long)0) { return(string.Empty); } //Allocate buffer + read byte[] pSource = new byte[ulSizeRequired]; pStream.Read(pSource, (int)ulSizeRequired, IntPtr.Zero); #region Auto-Encoding (Работает не всегда) ////UTF-8: EF BB BF ////UTF-16 big endian byte order: FE FF ////UTF-16 little endian byte order: FF FE ////UTF-32 big endian byte order: 00 00 FE FF ////UTF-32 little endian byte order: FF FE 00 00 //Encoding enc = null; //if (pSource.Length > 8) //{ // // Check byte order mark // if ((pSource[0] == 0xFF) && (pSource[1] == 0xFE)) // UTF16LE // enc = Encoding.Unicode; // if ((pSource[0] == 0xFE) && (pSource[1] == 0xFF)) // UTF16BE // enc = Encoding.BigEndianUnicode; // if ((pSource[0] == 0xEF) && (pSource[1] == 0xBB) && (pSource[2] == 0xBF)) //UTF8 // enc = Encoding.UTF8; // if (enc == null) // { // // Check for alternating zero bytes which might indicate Unicode // if ((pSource[1] == 0) && (pSource[3] == 0) && (pSource[5] == 0) && (pSource[7] == 0)) // enc = Encoding.Unicode; // } //} //if (enc == null) enc = Encoding.UTF8; //int bomLength = enc.GetPreamble().Length; //return enc.GetString(pSource, bomLength, pSource.Length - bomLength); #endregion return(Encode.GetString(pSource)); }
public static String GetDocumentSource(ref HTMLDocument doc, Encoding enc) { if (doc == null) { return(null); } bool IsUnicodeDetermined = false; Encoding theEncoding = enc; if (theEncoding == null) { theEncoding = Encoding.GetEncoding(0); //Windows default } if (theEncoding != Encoding.GetEncoding(0)) { //Don't try to detect unicode if we were //passed an encoding other than the default IsUnicodeDetermined = true; } // use the routine from htmlwrapper MemoryStream memstream = new MemoryStream(); ComStream cstream = new ComStream(memstream); IPersistStreamInit pStreamInit = (IPersistStreamInit)doc; pStreamInit.Save(cstream, false); StringBuilder Result = new StringBuilder(); //goto start of stream memstream.Seek(0, SeekOrigin.Begin); int iSize = 2048; byte[] bytedata = new byte[2048]; int iBOMLength = 0; while (true) { iSize = memstream.Read(bytedata, 0, bytedata.Length); if (iSize > 0) { if (!IsUnicodeDetermined) { //look for byte order mark bool IsUTF16LE = false; bool IsUTF16BE = false; bool IsUTF8 = false; bool IsBOMPresent = false; if ((bytedata[0] == 0xFF) & (bytedata[1] == 0xFE))//UTF16LE { IsUTF16LE = true; IsBOMPresent = true; } if ((bytedata[0] == 0xFE) & (bytedata[1] == 0xFF))// UTF16BE { IsUTF16BE = true; IsBOMPresent = true; } if ((bytedata[0] == 0xEF) & (bytedata[1] == 0xBB) & (bytedata[2] == 0xBF)) //UTF8 { IsUTF8 = true; IsBOMPresent = true; } //look for alternate zeroes if (!IsUTF16LE & !IsUTF16BE & !IsUTF8) { if ((bytedata[1] == 0) & (bytedata[3] == 0) & (bytedata[5] == 0) & (bytedata[7] == 0)) { IsUTF16LE = true; //best guess } } if (IsUTF16LE) { theEncoding = Encoding.Unicode; } else if (IsUTF16BE) { theEncoding = Encoding.BigEndianUnicode; } else if (IsUTF8) { theEncoding = Encoding.UTF8; } if (IsBOMPresent) { //strip out the BOM iBOMLength = theEncoding.GetPreamble().Length; } //don't repeat the test IsUnicodeDetermined = true; } Result.Append(theEncoding.GetString(bytedata, iBOMLength, iSize)); } else { break; } } memstream.Close(); return(Result.ToString()); }
public override bool GetBuffer(GetBufferCallback callBack) { this.getBufferCallback = callBack; if (this.buffer != null && callBack != null) { callBack(this.buffer); return(true); } if (this.m_pIWebBrowser2 == null) { SetState(BrowserStatus.Error); isRefreshingForBuffer = false; return(false); } IHTMLDocument2 document = this.m_pIWebBrowser2.Document as IHTMLDocument2; if (document == null) { SetState(BrowserStatus.Error); isRefreshingForBuffer = false; return(false); } IPersistStreamInit ips = document as IPersistStreamInit; if (ips == null) { SetState(BrowserStatus.Error); isRefreshingForBuffer = false; return(false); } try { IStream istream; CreateStreamOnHGlobal(IntPtr.Zero, true, out istream); if (istream == null) { SetState(BrowserStatus.Error); isRefreshingForBuffer = false; return(false); } int cb; unsafe { int hr = ips.Save(istream, false); if (hr != HRESULT.S_OK) { Marshal.ThrowExceptionForHR(hr); SetState(BrowserStatus.Error); return(false); } istream.Seek(0, 0, IntPtr.Zero); byte[] buffer = new byte[1024]; StringBuilder sb = new StringBuilder(); int * pcb = &cb; IntPtr readOffset = new IntPtr(pcb); do { istream.Read(buffer, 1024, readOffset); sb.Append(System.Text.ASCIIEncoding.UTF8.GetString(buffer, 0, cb)); } while (cb > 0); istream = null; ips = null; if (sb.Length <= 0) { SetState(BrowserStatus.Error); isRefreshingForBuffer = false; return(false); } isRefreshingForBuffer = false; this.buffer = sb.ToString(); } } catch (Exception e) { // workaround a bug on IE // when there's a "no-cache" http header // the HTML Source is invisible, so to fix it, // we need to refresh the page.. if (e is FileNotFoundException) { // Call refresh unless called before... if (isRefreshingForBuffer || m_pIWebBrowser2 == null) { SetState(BrowserStatus.Error); return(false); } isRefreshingForBuffer = true; SetState(BrowserStatus.Processing); m_pIWebBrowser2.Refresh(); return(true); } else { SetState(BrowserStatus.Error); return(false); } } if (callBack != null) { callBack(this.buffer); } return(true); }