怎样获得网页已显示图像的图像数据?

gbstar2021 2002-04-11 08:15:19
1.我要把网页上的图像保存到数据库中.IHTMLImgElement中有没有获得图像数据的方法?


另外一个问题:怎样才能把网页包含的所有数据打包保存成一个文件?(就像 mht 那样)
...全文
131 3 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
蒋晟 2002-04-19
HOWTO: Programmatically Copy an IMG Element to the Clipboard

Q293125


--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Internet Explorer (Programming) versions 4.0, 4.01, 4.01 SP1, 4.01 SP2, 5, 5.01, 5.01 SP1, 5.5, 5.5 Service Pack 1
Microsoft Visual Studio, Enterprise Edition 6.0

--------------------------------------------------------------------------------


SUMMARY
This article illustrates how to programmatically copy an image on a Web page (an IMG element) to the clipboard.



MORE INFORMATION
The best way to copy an image on a Web page to the clipboard is to use the execCommand method of the controlRange object. The following code illustrates to do this from within script on the page itself (given the ID of the image to be copied):


function copyImage(sImgID)
{
var ctrlRange = document.body.createControlRange();
ctrlRange.add(document.all(sImgID));
ctrlRange.execCommand("Copy");
}
In Microsoft Visual C++, this method call translates to IHTMLControlRange::execCommand. The following code illustrates how to implement the same technique in Visual C++ given an IDispatch pointer to the document that contains the image and the ID of the IMG element wrapped in a VARIANT structure (with a type VT_BSTR):

STDMETHODIMP CMyBrowser::CopyImage(LPDISPATCH pDispDoc, VARIANT vImageID)
{
HRESULT hr = E_FAIL;
IHTMLDocument2* pDoc = NULL;
IHTMLElement* pelmBody = NULL;
IHTMLElement2* pelmBodyTwo = NULL;
IDispatch* pdispImgElement = NULL;
IDispatch* pdispCtrlRange = NULL;
IHTMLElementCollection* pColl = NULL;
IHTMLControlElement* pCtrlElement = NULL;
IHTMLControlRange* pCtrlRange = NULL;
BSTR bstrCommand = SysAllocString(L"Copy");
VARIANT_BOOL vbReturn;
VARIANT vEmpty;
VariantInit(&vEmpty);

if (pDispDoc == NULL)
goto Cleanup;

if (FAILED(pDispDoc->QueryInterface(IID_IHTMLDocument2, (void**) &pDoc)))
goto Cleanup;

if (FAILED(pDoc->get_all(&pColl)))
goto Cleanup;

if (FAILED(pColl->item(vImageID, vEmpty, &pdispImgElement))
|| pdispImgElement == NULL)
goto Cleanup;

if (FAILED(pDoc->get_body(&pelmBody)) || pelmBody == NULL)
goto Cleanup;

if (FAILED(pelmBody->QueryInterface(IID_IHTMLElement2, (void**) &pelmBodyTwo))
|| pelmBodyTwo == NULL)
goto Cleanup;

if (FAILED(pelmBodyTwo->createControlRange(&pdispCtrlRange))
|| pdispCtrlRange == NULL)
goto Cleanup;

if (FAILED(pdispCtrlRange->QueryInterface(IID_IHTMLControlRange, (void**) &pCtrlRange))
|| pCtrlRange == NULL)
goto Cleanup;

if (FAILED(pdispImgElement->QueryInterface(IID_IHTMLControlElement, (void**) &pCtrlElement))
|| pCtrlElement == NULL)
goto Cleanup;

hr = pCtrlRange->add(pCtrlElement);

if (SUCCEEDED(hr))
hr = pCtrlRange->execCommand(bstrCommand, VARIANT_FALSE, vEmpty, &vbReturn);

pCtrlElement->Release();
hr = S_OK;

Cleanup:

SysFreeString(bstrCommand);

if (pCtrlRange)
pCtrlRange->Release();

if (pdispCtrlRange)
pdispCtrlRange->Release();

if (pelmBodyTwo)
pelmBodyTwo->Release();

if (pelmBody)
pelmBody->Release();

if (pdispImgElement)
pdispImgElement->Release();

if (pColl)
pColl->Release();

if (pDispDoc)
pDispDoc->Release();

return hr;
}


  • 打赏
  • 举报
回复
蒋晟 2002-04-12
FindCloseUrlCache Closes the specified enumeration handle.
FindFirstUrlCacheEntry Begins the enumeration of the cache.
FindFirstUrlCacheEntryEx Begins a filtered enumeration of the cache.
FindNextUrlCacheEntry Retrieves the next entry in the cache.
FindNextUrlCacheEntryEx Retrieves the next entry in a filtered cache enumeration.
GetUrlCacheEntryInfo Retrieves information about a cache entry.
GetUrlCacheEntryInfoEx Searches for the URL after translating any cached redirections that would be applied in offline mode by HttpSendRequest.
ReadUrlCacheEntryStream Reads the cached data from a stream that has been opened using RetrieveUrlCacheEntryStream.
RetrieveUrlCacheEntryFile Retrieves a cache entry from the cache in the form of a file.
RetrieveUrlCacheEntryStream Provides the most efficient and implementation-independent way of accessing the cache data.
  • 打赏
  • 举报
回复
ju_feng 2002-04-11
网页中的图象都是以图象文件的形式载入的。你找到文件所在位置(通过src=8.jpg等标志)再读取他即可!
  • 打赏
  • 举报
回复
相关推荐
发帖
VC/MFC
加入

1.6w+

社区成员

VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
申请成为版主
帖子事件
创建了帖子
2002-04-11 08:15
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……