Compress picture and photos to store in SQL Server database - c#

I want to save many pictures into a SQL Server database file. How can I compress pictures to store in database? What technologies can I use?
Please help me
After Edit:
How Can I Compress And Resize Pictures to Store in Sql Server DataBase?

Pictures don't compress much if they already in jpg and most other formats. You could try compressing them further before sending to SQL Server but it probably isn't worth the effort, unless you have BMPs.
I suggest you look at FILESTREAM to store them in SQL Server (assuming SQL Server 2008) which is more efficient for this kind of data.

You could save them in JPEG and include the file. For doing it, you can use Image.Save method. And if you want to set the quality you can use Encoder.Compression.
You should also make sure that the resolution of the images is not too much.

You can store pictures in sql database by convert images to byte array.You can do it with below code :
byte[] ReadFile(string sPath)
{
byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;
}

Related

Storing files in SQL Server database

I want to store files in my SQL Server database by C# which I have done it without problem.
This is my code:
byte[] file;
using (var stream = new FileStream(letter.FilePath, FileMode.Open, FileAccess.Read))
{
using (var reader = new BinaryReader(stream))
{
file = reader.ReadBytes((int)stream.Length);
letter.ltr_Image = file;
}
}
LetterDB letterDB = new LetterDB();
id = letterDB.LetterActions(letter);
The insert SQL action in the LetterActions module. But I want to know, in order to reduce the size of the database (which increases daily) is there any solution for compressing the files and then store them in the database?
Yes , you can zip your files before storing them in the database, using the ZipFile class. Take a look here: https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile(v=vs.110).aspx
Plenty of sample code out there too. See here:http://imar.spaanjaars.com/414/storing-uploaded-files-in-a-database-or-in-the-file-system-with-aspnet-20
You can compress file like this. Then insert compressed file stream into DB, but when you read it you need decompress it.
If you really need store file in DB, suggest you compress and decompress it by client.
And better way handle file is store them in disk, and only store file path in DB, when client need file use file path get file.

How to store image from picturebox to database, in sql server 2008 using c# (VS 2010)

im new to c#, can somone please tell me how to add image to database, from picture box using c#.
i have a registration form which user is added to database(SQL 2008), but i have no idea how to add image of the user to database, which contain many columns of user information and a column of picture.
sql<2008> visual studio <2010>
You should have a binary field in DB for that. Read your image like binary array and save in into DB. But It is not goog practice as for me. I usually save image in cloud or folder and in DB - only URL
create table IMAGELOAD
(img1 binary)
while saving send path of the file to this field
Suggestion :
Instead saving image into database.Save in application folder.
Can you be more specific?
What you don't know how to do?
How to get image from pictureBox? This can help you, hope.
//Save content of imageBytes to db VARBINARY(MAX)
byte[] imageBytes;
using (imgStr = new System.IO.MemoryStream())
{
pictureBox.Image.Save(imgStr, System.Drawing.Imaging.ImageFormat.Jpeg); // Depending on your format.
imageBytes = imgStr.ToArray();
}
//to load from db use
using (Stream imgStr = new MemoryStream(imageBytes))
{
pictureBox.Image = System.Drawing.Image.FromStream(imgStr);
}

Creating Images from SQL Server BLOBs is painfully slow

Has anyone had any issues when converting a BLOB (jpg) from SQL Server 2008 to a file on your file system? It's taking upward of 5-8 seconds for the blob to be retrieved then file created
ultimately I'm gonna store the filepaths and do quick lookups like that but for the initial hit per jpeg, it takes a long time to create the file and I have to assume there's a way to make it faster or I'm just doing it wrong or in an inefficient way in C#.
Here's how I'm getting it from the DB:
memberPhoto.PhotoBinary = (byte[])reader.GetValue(ordinals[(int)Enums.MemberPhotoColumn.PhotoBinary])
Here's how I'm creating the file off that:
using (FileStream file = File.Create(photoPhysicalFilepath))
{
BinaryWriter writer = new BinaryWriter(file);
writer.Write(photoBinary);
writer.Flush();
}
return relativePhotoFilePath;
photoBinary is the byte[] value I got from the datareader.
Not sure if this is efficient. I've seen people go this route but don't understand if this is an old way to do it and I've found a more syntactically elegant way of doing it or if my way is just a more elegant way I've found:
fs = new FileStream(FilePath, FileMode.Create);
fs.Write(imageData, 0, imageData.Length);
fs.Close();
MimeType = image.MIMEType;
where imageData is again a byte[]
So I'm wondering if anyone has done it my way or do you typically go with the second syntax...has there been any performance gains from the second vs. first on a large scaled website that anyone has encountered?
ultimately just trying to figure out why it's taking so long for the blob to file creation.

How to store an image in a SQL Server database from Silverlight app?

I am trying to store some pictures into my SQL Server database from a Silverlight project, and I need some help, so my questions are:
How to convert an image to binary from a url to store it into my database (store all the image and not only the url)
Are there any other solutions, without passing by binary type? (since it exist the image type in SQL Server)
Finally, when the image is stored, how to read it from Silverlight?
Thank you in advance .
You'll want to convert the System.Drawing.Image to a byte array and save the byte array to the database.
System.Drawing.Image image;
System.IO.MemoryStream imageStream;
byte[] imageBytes;
// image = your image object
imageStream = new System.IO.MemoryStream();
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg); // Use whatever format your image is.
imageBytes = imageStream.ToArray();
// Save imageBytes to a DB column of type VARBINARY(MAX)
To get the data back into an System.Drawing.Image object from a byte array use System.Drawing.Image.FromStream(System.IO.Stream stream).
Download file contents and put into memory stream. See : http://www.csharp-examples.net/download-files/
Add bytes from memory stream into database
Get image with silverlight in C# by using the image class and method FromStream see http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx
http://www.pitorque.de/MisterGoodcat/post/Storing-images-in-SQL-Server-with-RIA-Services.aspx
It is fully functional as in the user can select images from the local disk and upload them to the service where they are stored in the database. The user can retrieve a list of all images in the database and download them to the client for watching, and they can delete existing images from the database.

an entire DataTable object to byte then saved in database

im tasked to create a virtual database that clients can create from our web application and have them save data to.
the problem now is to have it stored for later re-use.
im thinking of dynamically creating a DataTable object in c# then convert it to byte[]. now i want to know if this would be practical to save on a database...
is this possible?
You can use WriteXml to write to a stream:
byte[] raw;
using(MemoryStream ms = new MemoryStream()) {
table.WriteXml(ms);
raw = ms.ToArray();
}
...
raw = ...
using(MemoryStream ms = new MemoryStream(raw)) {
table.ReadXml(ms);
}
And write the byte[] to a varbinary(max) / image / etc column in a database if that is what you need (you mentioned database) - or just use xml.
If you are writing to a file, then just use a FileStream in place of a MemoryStream.

Categories