我需要获取图像的二进制数据 [英] I need to get the binary data for an image

查看:66
本文介绍了我需要获取图像的二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我上传了一张图片,然后根据需要使用Image resizer和imazen.crop裁剪上传的图片。现在我需要将裁剪的图像存储到sql server中。所以我需要将裁剪后的图像的二进制数据存储到数据库。

任何人都可以帮助我。



我可以获取上传图片的二进制数据。代码如下...

那么,现在如何获取被裁剪图像的二进制数据?



我尝试了什么:



Actually I uploaded a image and then Crop the uploaded image as desired using Image resizer and imazen.crop.Now i need to store the cropped image into sql server.So I need the binary data for the cropped image to be stored into the database.
Can Anyone Please help me.

I can get the binary data for the uploaded image.Code is below...
So, now how to get the binary data for the image that is cropped?

What I have tried:

int length = FileUpload1.PostedFile.ContentLength;
//create a byte array to store the binary image data
 byte[] imgbyte = new byte[length];
 //store the currently selected file in memeory
  HttpPostedFile img = FileUpload1.PostedFile;
  //set the binary data
 img.InputStream.Read(imgbyte, 0, length);
 string sql = "Insert into Files(FileName,FileContent,Keyword)values('" + txtImage.Text + "', @FileContent ,'" + TextBox1.Text + "')";
 SqlCommand cmd = new SqlCommand(sql, con);
 cmd.Parameters.AddWithValue("@FileContent", SqlDbType.Image).Value = imgbyte;

推荐答案

从不以这种方式做SQL开始吧!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



实际转换非常简单:



Start off by never doing SQL that way! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

The actual conversion is pretty simple:

MemoryStream ms = new MemoryStream();
myImage.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
byte[] myImageData = ms.ToArray();


这篇关于我需要获取图像的二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆