【问题标题】:Storing and retrieving images from Database从数据库存储和检索图像
【发布时间】:2012-01-13 06:53:20
【问题描述】:

我创建了一个 ASP.NET/C# 应用程序来将图像上传到 MySQL 数据库。该过程执行没有任何错误,但对于我上传的任何图像,我都会得到 100x100 白色图像的输出。我遵循的程序是。

1) 使用字段picture 创建了一个数据库并输入Binary(255)

2) 上传图片为cmd.Parameters.Add("@picture", OdbcType.Binary, 255).Value = FileUpload1.FileBytes;

3) 插入一个新记录并生成一个低于类型的值。 89504e470d0a1a0a0000000d49484452000002600000010008020000009b155d400000100049444154789cd4dc05745b57a2377a15c26088d99651b2248b999959b2c0966cc9cccccc8e1ddb01439899d3a4499a869999e33037d0340d34d4b4d5dbaee7e6f6b5337367eefad67bf3adf55f676dd98e221f6b9ddffeef738e20db5cbf826c77fd3638d8eafa6587ebd79daedfc0f6afd9eefae5ab372fd6bf7db9e5e7b7075dae4daf5e1c76b98ebb5cfb7ef935a5b31b028b32ea53f6ec3a77efe60fb919156e34222b297ee3aedd2e97ebe6dd870b96acd8b0efc0891bb76ae7ce8ba9a8dc70f1f2c917afaeb95ce75c1f276cd988b0180329c4c4aaf2d2

//--------------- 上传模块完成-----//

1) 使用

创建了一个 ASPX 页面

<asp:Image ID="img" runat="server" ImageUrl="~/MyImage.ashx" />

2) 留下没有任何代码的 ASPX.CS 文件

3) 添加了一个带有

的 ASHX 文件
<%@ WebHandler Language="C#" Class="MyImage" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing.Imaging;

public class MyImage : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/png";
        var data = "89504e470d0a1a0a0000000d49484452000002600000010008020000009b155d400000100049444154789cd4dc05745b57a2377a15c26088d99651b2248b999959b2c0966cc9cccccc8e1ddb01439899d3a4499a869999e33037d0340d34d4b4d5dbaee7e6f6b5337367eefad67bf3adf55f676dd98e221f6b9ddffeef738e20db5cbf826c77fd3638d8eafa6587ebd79daedfc0f6afd9eefae5ab372fd6bf7db9e5e7b7075dae4daf5e1c76b98ebb5cfb7ef935a5b31b028b32ea53f6ec3a77efe60fb919156e34222b297ee3aedd2e97ebe6dd870b96acd8b0efc0891bb76ae7ce8ba9a8dc70f1f2c917afaeb95ce75c1f276cd988b0180329c4c4aaf2d2";
        var buffer = StringToByteArray(data);
        context.Response.OutputStream.Write(buffer, 0, buffer.Length);
    }

    private byte[] StringToByteArray(string hex)
    {
        return Enumerable
            .Range(0, hex.Length)
            .Where(x => x % 2 == 0)
            .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
            .ToArray();
    }
    public bool IsReusable
    {
        get { return false; }
    }
}

通过执行此代码,一个白色的 100x100 图像将显示在输出中,用于任何彩色输入。我仔细检查了ContentType 并上传了各种尺寸和格式的图像,但无论我做什么,我都会得到相同的白色图像的输出。我的代码有什么问题?有什么更正吗?

【问题讨论】:

  • 不是 MySQL 专家,但您是否正在创建最大长度为 255 字节的列?因此最大图像大小确实是 255 字节?
  • MySQL 不允许二进制长度超过 255

标签: c# asp.net image-processing


【解决方案1】:

您可以尝试将图像保存为 Base64 编码字符串,然后在您希望它在网页上呈现时将其转换回图像。

这是一个关于如何做的链接。

Base64String to Image and visa versa

这是我在项目中如何使用它的link,不是完全来自图像或数据库,但概念是相同的。

在您的网页中使用我的方法时,您将使用

<img src="${downloadurl}" />

【讨论】:

    【解决方案2】:

    255 字节的二进制字段类型不会大到足以处理除了非常小的图像之外的任何内容。检查您要上传的文件的大小并相应地重新调整字段的大小。

    【讨论】:

      【解决方案3】:

      我在 ashx 中有这段代码,它从 MSSQL 数据库中提取图像数据并显示它

      public void ProcessRequest(HttpContext context)
          {
              Guid picid;
              if (context.Request.QueryString["picid"] != null)
                  picid = new Guid(context.Request.QueryString["picid"]);
              else
                  throw new ArgumentException("No parameter specified");
      
              context.Response.ContentType = "image/jpeg";
              Stream strm = ShowPicImage(picid);
              byte[] buffer = new byte[4096];
              int byteSeq = strm.Read(buffer, 0, 4096);
      
              while (byteSeq > 0)
              {
                  context.Response.OutputStream.Write(buffer, 0, byteSeq);
                  byteSeq = strm.Read(buffer, 0, 4096);
              }
          }
      
          public Stream ShowPicImage(Guid piciddata)
          {
              ProductPicture pictureData = new ProductPicture("ProductPictureID", piciddata);
              object img = pictureData.Data;
              try
              {
                  return new MemoryStream((byte[])img);
              }
              catch
              {
                  return null;
              }
          }
      

      我不知道您是否可以尝试解决您的问题。 (其中 pictureData.Data 属性类型为 byte[],相关数据库列为 varbinary(max))

      更新答案

      您可能还想考虑将数据库中的数据存储为 BLOB

      【讨论】:

        猜你喜欢
        • 2011-03-21
        • 1970-01-01
        • 1970-01-01
        • 2015-03-16
        • 2015-08-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-30
        • 1970-01-01
        相关资源
        最近更新 更多