?
Solved

Resize Documents before saving in Sql Server

Posted on 2012-04-11
8
Medium Priority
?
270 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

649 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