在 asp.net 中将上传的文件转换为位图图像

发布于 2024-11-09 08:23:16 字数 272 浏览 0 评论 0原文

我有一个 FileUpload 框和 按钮。在我的场景中,要上传的文件是图像文件。我想将这些图像文件转换为位图并将它们临时存储在缓冲区中。

我有一个函数,它接受两个位图输入并告诉我们这两个文件是否匹配。

其中一个文件将来自 ButtonClick 事件上的 FileUpload 控件,另一个位图将从数据库读取。

谁能告诉我如何将这些文件转换为位图并将这两个位图对象传递给函数。

I have a FileUpload box and button. In my scenario the files that are going to be uploaded are image files. I want to convert these image files into bitmaps and store them temporarily in a buffer.

I have a function which takes two bitmap inputs and tells us whether the two files match or not.

One of the file will come from the FileUpload control on ButtonClick event and the other bitmap will be read from Database.

Can anyone tell me as to how do I convert these files to bitmaps and pass both the bitmap objects to the function.

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

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

发布评论

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

评论(1

極樂鬼 2024-11-16 08:23:16

您可以按如下方式获取上传图像的位图:

System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(userFileUpload.PostedFile.InputStream);

然后获取存储的副本(希望将其存储为字节数组,并且您有一个 ID 来获取它),然后将其转换为位图,如下所示将

byte[] byteArrayStoredImage = ImageService.GetImageData(imageID);
MemoryStream imgStream = new MemoryStream(byteArrayStoredImage);
System.Drawing.Bitmap bmpStoredImage = new Bitmap(imgStream);

两个位图手(bmpPostedImage和bmpStoredImage),然后可以调用该函数进行比较。首先,您可以从 http://www.dreamincode.net/code/snippet2859 尝试此功能。 htm 看看效果如何。可能有更有效的函数可以进行比较,尝试谷歌搜索将是一个很好的尝试。

编辑

下面找到从数据库检索图像的代码,假设我在下面的注释中放入:

    public byte[] GetImageData(string imageID)
    {
                string connectionString = ConfigurationManager.ConnectionStrings["connectionstringname"];
        SqlConnection connection = SqlConnection(connectionString);
        connection.Open();
        SqlCommand command1 = new SqlCommand("select imgfile from myimages where imgname=@imageId", connection);
        SqlParameter myparam = command1.Parameters.Add("@imageId", SqlDbType.NVarChar, 30);
        myparam.Value = imageID;
        byte[] img = (byte[])command1.ExecuteScalar();
        connection.Close();
        return img;
    }

然后将 ImageService.GetImageData(imageID) 更改为 GetImageData(imageID);

另请注意,此处未解决错误处理问题,因此可能必须将其纳入最终代码中。

You can get a bitmap of your uploaded image as follows:

System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(userFileUpload.PostedFile.InputStream);

You then get the stored copy (which hopefully is stored as a byte array and you have an ID to get it), and then convert it to bitmap as follows

byte[] byteArrayStoredImage = ImageService.GetImageData(imageID);
MemoryStream imgStream = new MemoryStream(byteArrayStoredImage);
System.Drawing.Bitmap bmpStoredImage = new Bitmap(imgStream);

With the two bitmaps in hand (bmpPostedImage and bmpStoredImage), you can then call the function to do the comparison. For a start you can try this function from http://www.dreamincode.net/code/snippet2859.htm and see how it it goes. there may be more efficient functions out there to do the comparison, trying out a google search for that will be a goo thing to try.

EDIT

Find below the code to retrieve image from the database with the assumptions I put in the comment below:

    public byte[] GetImageData(string imageID)
    {
                string connectionString = ConfigurationManager.ConnectionStrings["connectionstringname"];
        SqlConnection connection = SqlConnection(connectionString);
        connection.Open();
        SqlCommand command1 = new SqlCommand("select imgfile from myimages where imgname=@imageId", connection);
        SqlParameter myparam = command1.Parameters.Add("@imageId", SqlDbType.NVarChar, 30);
        myparam.Value = imageID;
        byte[] img = (byte[])command1.ExecuteScalar();
        connection.Close();
        return img;
    }

and then change ImageService.GetImageData(imageID) to GetImageData(imageID);

Note also, that error handling is not addressed here, so might have to factor that into your final code.

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