?
Solved

Resize Documents before saving in Sql Server

Posted on 2012-04-11
8
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

762 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