Solved

Resize Documents before saving in Sql Server

Posted on 2012-04-11
8
252 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

11 Experts available now in Live!

Get 1:1 Help Now