Solved

Resize Documents before saving in Sql Server

Posted on 2012-04-11
8
257 Views
Last Modified: 2012-04-17
Hello,

  In my web app, I have a screen where the users can upload  their documents. I should allow  the file formats of .pdf,jpg,png etc. I want the documents to be resized before they go in to the database. Having said that, I don't want to loose the quality of the file being resized.  
Could you let me know the best way to do this?

Right now I am using this. It is resizing, but I am loosing the quality.
  public byte[] resizeDocument(System.Drawing.Image image_file)
        {

            int image_height = image_file.Height;
            int image_width = image_file.Width;
            int max_height = 120;
            int max_width = 160;


            image_height = (image_height * max_width) / image_width;
            image_width = max_width;

            if (image_height > max_height)
            {
                image_width = (image_width * max_height) / image_height;
                image_height = max_height;
            }
            else
            {
            }

            Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height);
            System.IO.MemoryStream stream = new System.IO.MemoryStream();

            bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            stream.Position = 0;

            byte[] data = new byte[stream.Length + 1];
            stream.Read(data, 0, data.Length);

            return data;
}

Open in new window


Thanks
0
Comment
Question by:shahjagat
  • 4
  • 4
8 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37833867
one way is to use gzipstream but that will zip your file, see if its ok for you:-

http://www.codeproject.com/Articles/27203/GZipStream-Compress-Decompress-a-string
http://www.dotnetperls.com/gzipstream
0
 

Author Comment

by:shahjagat
ID: 37853296
BuggyCoder,

  I used the GZipStream to compress the file before saving it in to the database. It didn't give me any error BUT the size of the file is not decreasing in the database. The main reason why I am trying to compress or resize the file is, I don't want the file to use much space in the database.

Any suggestions?

Thanks
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 250 total points
ID: 37853364
see basically you have different types of files, gzip will work great for text files but will not do anything for jpg/png files.

I would rather suggest, not to save file in database, save it on disk and save its path in database. That's a more preferred approach...

This way you don't need to resize as you can directly store what has been uploaded on disk itself. Believe me, its much faster than going DB way.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:shahjagat
ID: 37853477
First problem i could think about saving it on disk and its path in DB is,  this web app would be browsed from ipad or any other tablet too. I am not sure if this approach may lead in to problems , if used on ipad. However, I will give it a try.

Can you think of any other ways to resize the file without loosing quality other than this GZipStream?

Appreciate the help
Thanks
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37853489
if its a web app hosted on web server, it doesn't matter where it is browsed from. Any files uploaded will be saved on Web Server's disk....

I don't think there is any standard way to achieve compression for all file types....
0
 

Author Comment

by:shahjagat
ID: 37853509
Yes, it is a web app. When you say "save it on disk and save its path in database" , you mean save it on the server's disk?
0
 

Author Comment

by:shahjagat
ID: 37853613
And don't you think that it will take more space if there are users in thousands and if every user has multiple files to upload.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37854294
it is going to take the same space on db as well. the db files are also stored on disk, so it is anyhow going to take space on disk only....
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question