zoukankan      html  css  js  c++  java
  • C++根据图片url下载图片

    需要使用到URLDownloadToFile()函数,该函数在头文件<urlmon.h>中声明。

    URLDownloadToFile()函数的定义如下:

    1 HRESULT URLDownloadToFile( 
    2     LPUNKNOWN pCaller,
    3     LPCTSTR szURL,
    4     LPCTSTR szFileName,
    5     DWORD dwReserved,
    6     LPBINDSTATUSCALLBACK lpfnCB
    7 );

    Parameters(参数含义):


    pCaller

    Pointer to the controlling IUnknown interface of the calling Microsoft ActiveX component (if the caller is an ActiveX component).

    Microsoft ActiveX控件的接口的指针,如果不是控件,则为NULL

    szURL

    Pointer to a string value containing the URL to be downloaded. Cannot be set to NULL

    要下载的url地址,不能为空

    szFileName

    Pointer to a string value containing the name of the file to create for bits that come from the download.

    下载后保存的文件名

    dwReserved

    Reserved. Must be set to 0.

    保留字段,必须为0

    lpfnCB

    Pointer to the caller's IBindStatusCallback interface. URLDownloadToFile calls this interface's IBindStatusCallback::OnProgress method on a connection activity, including the arrival of data. IBindStatusCallback::OnDataAvailable is never called.

    下载进度状态回调

     

    Return Value
    Returns one of the following values.
    S_OK : The download started successfully.
    E_OUTOFMEMORY: The buffer length is invalid, or there is insufficient memory to complete the operation.
    INET_E_DOWNLOAD_FAILURE:The specified resource or callback interface was invalid.
     
    由于函数的参数使用的是LPWSTR,而我们常用的是string,所以用到了MultiByteToWideChar()函数将string转为LPWSTR
     
    代码如下
     1 /*
     2 @author:CodingMengmeng
     3 @theme:C++根据图片url下载图片
     4 @time:2017-1-6 22:58:00
     5 @blog:http://www.cnblogs.com/codingmengmeng/
     6 */
     7 #include <tchar.h>
     8 #include <iostream>
     9 #include <urlmon.h>
    10 /*
    11 #pragma comment(lib,"urlmon.lib")作用:
    12 连接静态库到项目中,效果等同于:
    13 项目属性——链接器——输入——附加依赖项中加入这个lib
    14 (头文件<urlmon.h>中只是包含了数据结构和函数声明,是编译阶段;
    15 链接阶段将从静态库中恢复这些函数和数据并把他们和应用程序中的其它模块组合在一起生成可执行文件,
    16 该过程称为“静态链接”)
    17 */
    18 #pragma comment(lib,"urlmon.lib")
    19 using namespace std;
    20 
    21 int _tmain(int argc, char* argv[])
    22 {
    23     string url = "http://pic104.nipic.com/file/20160715/6171480_185807154956_2.jpg";
    24     size_t len = url.length();//获取字符串长度
    25     int nmlen = MultiByteToWideChar(CP_ACP, 0, url.c_str(), len + 1, NULL, 0);//如果函数运行成功,并且cchWideChar为零,
    26     //返回值是接收到待转换字符串的缓冲区所需求的宽字符数大小。
    27     wchar_t* buffer = new wchar_t[nmlen];
    28     MultiByteToWideChar(CP_ACP, 0, url.c_str(), len + 1, buffer, nmlen);
    29     HRESULT hr = URLDownloadToFile(NULL, buffer,_T("E:\C++lianxi\Blog\urlDownload2File\urlDownload2File\sky.jpg"), 0, NULL);
    30     if (hr == S_OK)
    31     {
    32         cout << "ok" << endl;
    33     }
    34     return 0;
    35 }

    运行结果

    程序输出ok,且在指定目录中也保存了下载到的图片。 

    以上。

  • 相关阅读:
    BPM实例方案分享:表单子表自动填入数据
    H3 BPM循环子表相关方法介绍
    H3 BPM前后台交互方法介绍
    Web Service Adapter简介:
    H3 BPM钉钉接入配置
    H3 BPM 跨平台表单发起详解
    H3 BPM门户操作说明及实例介绍
    H3 BPM报销流程开发示例
    Photon Cloud Networking: OnPhotonSerializeView Not Firing
    unity 事件顺序及功能说明
  • 原文地址:https://www.cnblogs.com/codingmengmeng/p/6258020.html
Copyright © 2011-2022 走看看