【问题标题】:Download image from picturebox.image in c#.net desktop application从 c#.net 桌面应用程序中的 picturebox.image 下载图像
【发布时间】:2020-07-08 21:16:09
【问题描述】:

我正在开发一个桌面应用程序,我想在其中执行特定任务,即将图像保存在数据库中从数据库中获取图像并在 PictureBox 中显示和从 Picturebox Tool 下载图像并保存在文件夹中

这是任务代码,

  1. 将图像保存在数据库中,因为我使用 ImageToBase64 函数以字符串格式保存在数据库中。喜欢:ImageToBase64(pictureBox1.Image, System.Drawing.Imaging.ImageFormat.Jpeg)

ImageToBase64 函数代码:

public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
{
    using (MemoryStream ms = new MemoryStream())
    {
        if (image != null)
        {
            // Convert Image to byte[]
            image.Save(ms, format);
            byte[] imageBytes = ms.ToArray();

            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        }
        else
        {
            return null;
        }
    }
}

现在从数据库中获取图像并在图片框中显示图像

picturebox1.image = Base64ToImage(dtSelStock.Rows[0]["DesignImage"].ToString())

Base64ToImage 函数代码:

public Image Base64ToImage(string base64String)
{
    if (base64String != "")
    {
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        MemoryStream ms = new MemoryStream(imageBytes, 0,
          imageBytes.Length);

        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        Image image = Image.FromStream(ms, true);
        return image;
    }
    else
    {
        return null;
    }
}

所以,这两个任务完美地工作。但是第三个也是最后一个任务是从图片框中将图像保存在文件夹中。我尝试了一些代码但没有正常工作,实际上显示错误,即

pictureBox1.Image.Save("D\\Image\\'" + pictureBox1.Image + "'", ImageFormat.JPEGImage);

【问题讨论】:

  • 更新您的问题以提供错误\异常
  • @BrettCaswell 已编辑。请检查它。
  • 您还应该能够通过 pictureBox2.image = Base64ToImage(ImageToBase64(pictureBox1.Image, System.Drawing.Imaging.ImageFormat.Jpeg)); 删除对可重现的数据库\数据表概念的引用来最小化此代码以便更轻松地重现它,对吧?
  • @BrettCaswell 实际上我正在通过 sql 查询获取数据表中的数据并从该数据表中显示图片框中的图像,但它的工作完全不是问题,
  • 您没有提供minimal reproducible example,也没有提供有关您遇到的任何错误的任何具体信息,因此应该关闭该问题。也就是说,根据您发布的内容,您未能在驱动器号之后包含 : 字符。 说,从PictureBox 保存确实是错误的。您应该保存最初存储在数据库中的确切字节。否则,每次保存图像时,都会稍微降低质量(因为您保存为 JPEG)。

标签: c# .net image desktop-application picturebox


【解决方案1】:

以下代码 sn-p 重现了您的问题。

   var dataString = ImageToBase64(pictureBox1.Image, System.Drawing.Imaging.ImageFormat.Jpeg);
   pictureBox2.Image = Base64ToImage(dataString);
   pictureBox2.Image.Save("D\\Images\\'" + pictureBox2.Image + "'", System.Drawing.Imaging.ImageFormat.Jpeg);

"D\\Image\\'" + pictureBox1.Image + "'"有关

我在运行这些 sn-ps 时收到以下异常堆栈跟踪

System.Runtime.InteropServices.ExternalException
  HResult=0x80004005
  Message=A generic error occurred in GDI+.
  Source=System.Drawing
  StackTrace:
   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at System.Drawing.Image.Save(String filename, ImageFormat format)
   at WindowsFormsApp1.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\brcaswell\Source\Repos\WindowsFormsApp1\WindowsFormsApp1\Form1.cs:line 25
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at WindowsFormsApp1.Program.Main() in C:\Users\brcaswell\Source\Repos\WindowsFormsApp1\WindowsFormsApp1\Program.cs:line 19

它不是有效的文件路径并且\或目录不存在。

这将纠正它

   var filePath = @"D:\Images\";
   Directory.CreateDirectory(filePath);
   pictureBox2.Image.Save(Path.Combine(filePath, $"'{pictureBox2.Image}'"), System.Drawing.Imaging.ImageFormat.Jpeg);

但是,您应该注意'{pictureBox2.Image}' 将导致类型的toString 并将'System.Drawing.Bitmap' 作为文件名。您应该更正它并包含文件扩展名

Path.Combine(filePath, "myImage.jpeg")

另外,正如@'Peter Duniho' 在他对这个问题的评论中提到的,您应该考虑以与图片框控件的图形上下文无关的方式保存此图像文件。


此外,您应该考虑将图像保存为数据库中的二进制 byte[],而不是 base64 字符串表示形式。

【讨论】:

    猜你喜欢
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多