Piy*_*ush 4 c# asp.net file-upload image bitmap
我有一个FileUpload盒子和button.在我的场景中,要上传的文件是图像文件.我想将这些图像文件转换为位图并将它们临时存储在缓冲区中.
我有一个函数,它接受两个位图输入,并告诉我们这两个文件是否匹配.
其中一个文件来自事件FileUpload控件,ButtonClick另一个位图将从数据库中读取.
任何人都可以告诉我如何将这些文件转换为位图并将位图对象传递给函数.
您可以按如下方式获取上传图片的位图:
System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(userFileUpload.PostedFile.InputStream);
Run Code Online (Sandbox Code Playgroud)
然后,您获取存储的副本(希望存储为字节数组,并且您有一个ID来获取它),然后将其转换为位图,如下所示
byte[] byteArrayStoredImage = ImageService.GetImageData(imageID);
MemoryStream imgStream = new MemoryStream(byteArrayStoredImage);
System.Drawing.Bitmap bmpStoredImage = new Bitmap(imgStream);
Run Code Online (Sandbox Code Playgroud)
通过手中的两个位图(bmpPostedImage和bmpStoredImage),您可以调用该函数进行比较.首先,您可以从http://www.dreamincode.net/code/snippet2859.htm尝试此功能,看看它是怎么回事.可能会有更有效的功能进行比较,尝试谷歌搜索,这将是一个goo尝试.
编辑
在下面找到从数据库中检索图像的代码,并在下面的注释中添加了以下假设:
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;
}
Run Code Online (Sandbox Code Playgroud)
然后将ImageService.GetImageData(imageID)更改为GetImageData(imageID);
另请注意,此处未解决错误处理问题,因此可能需要将其纳入最终代码.
| 归档时间: |
|
| 查看次数: |
17601 次 |
| 最近记录: |