如何将图像转换为二进制数据? [英] How to convert images to binary data ?

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

问题描述

Hii,



我的问题是我的应用程序中有很多图像,因此存储到图像文件夹并在运行时从那里恢复更快或保持sql中的二进制数据中的图像和从数据库中获取图像然后显示更快。



在这种情况下,如何使所有图像变为二进制以便我可以将所有数据存储到图像的数据库中。



比方说我有productdetails表.// =>请解释我这个



谢谢

解决方案

将图像转换为二进制数据可以像这样使用c#

  foreach  string  fileName < span class =code-keyword> in  Directory.GetFiles( @  C:\ SomePath  。jpg)) / /  例如 
{
使用(FileStream fs = < span class =code-keyword> new FileStream( < imagepath> ,FileMode.Open,FileAccess.Read))
{
byte [] binaryImage = new byte [fs.Lengt H];
fs.Read(binaryImage, 0 ,( int )fs.Length);
// 将BLOB存储到数据库中
}
}





但是,如果您的图像很大(如系统相机),您的数据库会增长很多,并且会使备份速度变慢,所以仅存储图像的路径并将它们保存在硬盘驱动器上可能更具吸引力。

这是双方之间的一场宗教战争,因此本研究报告可能会引起您的兴趣。做出自己的决定。

BLOB或不BLOB [ ^ ]


以图像格式存储照片,将它们转换为SQL中可用的字节数组,速度更快。我相信你一定找过一些。制作可以为所有图像完成的所有图像的循环。



参考此链接:使用Microsoft .NET从SQL Server存储和检索图像


在sql imagedata中必须是varbinary格式



byte [] FileData = File.ReadAllBytes(图像路径);

将其保存到数据库中



插入productdetails值(+ FileData +)


Hii ,

My question is i have lots of images in my application , so storing into image folder and retriving from there during run time is more faster or keeping the image in binary data in sql and fetching image from database and then display is much faster .

well in that case , how to make all images to binary so that i can store all data to database for the image .

Say for example i have productdetails table .// => please explain me this

Thanks

解决方案

Converting the image into binary data can be done like this using c#

foreach (string fileName in Directory.GetFiles(@"C:\SomePath", ".jpg")) // For example
{
    using (FileStream fs = new FileStream("<imagepath>", FileMode.Open, FileAccess.Read))
    {
        byte[] binaryImage = new byte[fs.Length];
        fs.Read(binaryImage, 0, (int)fs.Length);
        // Store the BLOB n your database
    }
}



However, your database will grow a lot if your images are large, like from a system camera, and will make backups slower, so storing only the path to the image and keep them on a hard drive might be more attractive.
This is a bit of religious war between the two sides, so this research paper might be of interest for you to make your own decision.
To BLOB or Not To BLOB[^]


Store your photos in 'Image' format coverting them to byte array that is available in SQL and is much faster. I am sure you must have searched some. Make a loop of all images that may be done for all images.

Refer this link : Storing and Retrieving Images from SQL Server using Microsoft .NET


in sql imagedata must be in varbinary format

byte[] FileData = File.ReadAllBytes(image path);
save this into database

insert into productdetails values("+FileData+")


这篇关于如何将图像转换为二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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