通过libcurl下载图像

发布于 2024-09-18 06:08:21 字数 2036 浏览 11 评论 0原文

我尝试通过 libcurl 下载图像(我知道其他下载选项,但我需要通过 libcurl 完成

)保存图片,打不开。 文件大小与我自己下载文件时不同。

using System;
using System.Windows.Forms;
using SeasideResearch.LibCurlNet;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            HTTP cURL = new HTTP();
            cURL.CurlInit();

            // Getting Data - Downloading the picture
            String data = cURL.HTTPGet("http://www.hcs.harvard.edu/csharp/Logo1.png");

            // Saving Picture
            HTTP.save_file("bilde2.jpg", data);
        }
    }

    class HTTP
    {
        public Easy easy;
        public string SockBuff;

        public void CurlInit()
        {
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
        }

        public string HTTPGet(string URL)
        {
            easy = new Easy();

            Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
            easy.SetOpt(CURLoption.CURLOPT_URL, URL);
            easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
            //easy.SetOpt(CURLoption.CURLOPT_WRITEDATA, f);
            easy.Perform();
            return SockBuff;
        }


        public Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
        {
            SockBuff = SockBuff + System.Text.Encoding.UTF8.GetString(buf);
            return size * nmemb;
        }

        static public void save_file(string file_name, string text_to_write)
        {
            using (FileStream stream = new FileStream(file_name, FileMode.Create))
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    //writer.Write("hello");
                    writer.Write(text_to_write);
                    writer.Close();
                }
            }
        }

    }
}

我在这里做错了什么?

I try to download image via libcurl (I know other options to download, but I need to get it done via libcurl)

When I download & save the image, I can not open it.
The file size is different as when downloading file myself.

using System;
using System.Windows.Forms;
using SeasideResearch.LibCurlNet;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            HTTP cURL = new HTTP();
            cURL.CurlInit();

            // Getting Data - Downloading the picture
            String data = cURL.HTTPGet("http://www.hcs.harvard.edu/csharp/Logo1.png");

            // Saving Picture
            HTTP.save_file("bilde2.jpg", data);
        }
    }

    class HTTP
    {
        public Easy easy;
        public string SockBuff;

        public void CurlInit()
        {
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
        }

        public string HTTPGet(string URL)
        {
            easy = new Easy();

            Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
            easy.SetOpt(CURLoption.CURLOPT_URL, URL);
            easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
            //easy.SetOpt(CURLoption.CURLOPT_WRITEDATA, f);
            easy.Perform();
            return SockBuff;
        }


        public Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
        {
            SockBuff = SockBuff + System.Text.Encoding.UTF8.GetString(buf);
            return size * nmemb;
        }

        static public void save_file(string file_name, string text_to_write)
        {
            using (FileStream stream = new FileStream(file_name, FileMode.Create))
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    //writer.Write("hello");
                    writer.Write(text_to_write);
                    writer.Close();
                }
            }
        }

    }
}

What am I doing wrong here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风和你 2024-09-25 06:08:21

每次调用函数 save_file 时,您的代码都会重新创建该文件。

因此,您应该检查该文件是否存在。

如果文件存在,FileStream 应该使用带有 Append 的 FileMode。

尝试以下代码:

static public void save_file(string file_name, string text_to_write)
{
  using (var stream = File.Exists(file_name) 
    ? new FileStream(file_name, FileMode.Append) 
    : new FileStream(file_name, FileMode.Create))
  {
    using (var writer = new BinaryWriter(stream))
    {
      //writer.Write("hello");
      writer.Write(text_to_write);
      writer.Close();
    }
  }
}

Every time the function save_file is called, the file is recreated by your code.

So, you should check the file is exists or not.

If the file is exists, FileStream should use the FileMode with Append.

Try the codes below:

static public void save_file(string file_name, string text_to_write)
{
  using (var stream = File.Exists(file_name) 
    ? new FileStream(file_name, FileMode.Append) 
    : new FileStream(file_name, FileMode.Create))
  {
    using (var writer = new BinaryWriter(stream))
    {
      //writer.Write("hello");
      writer.Write(text_to_write);
      writer.Close();
    }
  }
}
习ぎ惯性依靠 2024-09-25 06:08:21

字符串包含文本,该文本具有给定的编码(在您的情况下为 UTF8),您不能将 .png 文件视为 utf-8 编码文本。

重写 HTTPGet 和 save_file 方法以仅处理字节/字节数组。

Strings contain text, which have a given encoding (UTF8 in your case), you can't treat a .png file as being utf-8 encoded text.

Rewrite your HTTPGet and save_file methods to only deal with bytes/byte arrays.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文