Solved

Resize Documents before saving in Sql Server

Posted on 2012-04-11
8
253 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
 

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
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.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now