Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

resize image

Posted on 2011-09-23
19
Medium Priority
?
334 Views
Last Modified: 2012-05-12
http://www.telerik.com/community/forums/aspnet/upload/how-to-resize-image.aspx

I have this link but on that I can't keep same aspect ratio same. What I need to do?
0
Comment
Question by:1jaws
[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
19 Comments
 
LVL 27

Accepted Solution

by:
Shaun Kline earned 1000 total points
ID: 36588865
To keep the aspect ratio, you would need to calculate the change on one of the dimensions and apply that change to the other dimension. I.e. divide the new width for the image by the original width of the image. Then multiply this value to the original height of the image.
0
 

Author Comment

by:1jaws
ID: 36588886
thank you for replying.. is it possible to show that how I might be able to do that?
0
 
LVL 8

Assisted Solution

by:PeteEngineer
PeteEngineer earned 500 total points
ID: 36588890

public static Size ProportionalSize(Size imageSize, Size MaxW_MaxH)
            {
                double multBy= 1.01;
                double w= imageSize.Width;   double h= imageSize.Height;

                while(w < MaxW_MaxH.Width && h < MaxW_MaxH.Height)
                {
                    w= imageSize.Width*multBy;
                    h= imageSize.Height*multBy;
                    multBy= multBy+.001;
                }

                while(w > MaxW_MaxH.Width || h > MaxW_MaxH.Height)
                {
                    multBy= multBy-.001;
                    w= imageSize.Width*multBy;
                    h= imageSize.Height*multBy;
                }

                if(imageSize.Width < 1)
                    imageSize=new Size(imageSize.Width+-imageSize.Width+1, imageSize.Height-imageSize.Width-1);
                if(imageSize.Height < 1)
                    imageSize=new Size(imageSize.Width-imageSize.Height-1, imageSize.Height+-imageSize.Height+1);

                imageSize= new Size(Convert.ToInt32(w), Convert.ToInt32(h));
                return imageSize;
            }

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:1jaws
ID: 36588917
I have this code and how I need to put above code to this?
public bool ThumbnailCallback()  
{  
    return false;  
}  
 
//Create a thumbnail  
public void CreateThumb(string fileName, System.IO.Stream imageStream, int width, int height)  
{  
    System.Drawing.Image.GetThumbnailImageAbort thumbnailImageAbortDelegate = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);  
    using (Bitmap originalImage = new Bitmap(imageStream))  
    {  
        using (System.Drawing.Image thumbnail = originalImage.GetThumbnailImage(width, height, thumbnailImageAbortDelegate, IntPtr.Zero))  
        {  
            thumbnail.Save(fileName);  
        }  
    }  
}  
 
 
private void Button1_Click(object sender, System.EventArgs e)  
{  
    Telerik.WebControls.UploadedFile file = RadUpload1.UploadedFiles[0];  
    string targetFolder = "c:\\temp\\";  
    string suffix = "_thumb";  
    CreateThumb(  
            targetFolder +   
            string.Concat(file.GetNameWithoutExtension(), suffix, file.GetExtension()), file.InputStream,  
            300,   300);  
}

Open in new window

0
 
LVL 27

Expert Comment

by:Shaun Kline
ID: 36588922
Using the code from the example...

Note: you could use either the passed in width or height parameter to determine the aspect ratio.
public void CreateThumb(string fileName, System.IO.Stream imageStream, int width, int height)  
{  
    System.Drawing.Image.GetThumbnailImageAbort thumbnailImageAbortDelegate = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);  
    using (Bitmap originalImage = new Bitmap(imageStream))  
    {  
        int newHeight = originalImage.Height * (newWidth / originalImage.Width);
        using (System.Drawing.Image thumbnail = originalImage.GetThumbnailImage(width, newHeight, thumbnailImageAbortDelegate, IntPtr.Zero))  
        {  
            thumbnail.Save(fileName);  
        }  
    }  
}

Open in new window

0
 

Author Comment

by:1jaws
ID: 36588939
says newWidth doest exist in the context
0
 

Author Comment

by:1jaws
ID: 36588968
int newWidth = originalImage.Width;
0
 

Author Comment

by:1jaws
ID: 36588993
I put above didnt work...
0
 

Author Comment

by:1jaws
ID: 36589072
Shaun_Kline, I cant get this to work, we are in right direction but actually don't know how to make this code work for me...that my size of photos automatically will size to 300x300 without changing the aspect radio...Can you be able to paste the working code, if possible?
0
 
LVL 27

Expert Comment

by:Shaun Kline
ID: 36589171
Instead of newWidth, use the passed in parameter: width.
0
 

Author Comment

by:1jaws
ID: 36589212
what do you mean? use like that? instead of width.. I am totally confused here

int newHeight = originalImage.Height * (width / originalImage.Width);
0
 

Author Comment

by:1jaws
ID: 36589231
or like this?

int newHeight = originalImage.Height * (300 / originalImage.Width);
0
 

Author Comment

by:1jaws
ID: 36589583
that is not working..
0
 

Author Comment

by:1jaws
ID: 36589609
PeteEngineer, couldnt even get your code working either.. it complians about Size, saying are you missing  references...
0
 
LVL 13

Assisted Solution

by:jonnidip
jonnidip earned 500 total points
ID: 36589879
Try this method:
void ResizeImage(String InputFileName, Int32 MaxWidth, Int32 MaxHeight, String ThumbnailFileName)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                Int32 NewWidth = 300;
                Int32 NewHeight = -1;

                #region Resize
                using (System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(InputFileName))
                {
                    if (FullsizeImage.Width <= NewWidth)
                        NewWidth = FullsizeImage.Width;

                    NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
                    if (NewHeight > MaxHeight)
                    {
                        // Resize with height instead
                        NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
                        NewHeight = MaxHeight;
                    }

                    // Save resized picture
                    using (System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero))
                        NewImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                #endregion

                ms.Position = 0;

                #region Write thumbnail file
                if (!Directory.Exists(Path.GetDirectoryName(ThumbnailFileName)))
                    Directory.CreateDirectory(Path.GetDirectoryName(ThumbnailFileName));
                FileInfo fi = new FileInfo(ThumbnailFileName);
                File.WriteAllBytes(ThumbnailFileName, ms.GetBuffer());
                #endregion
            }
        }

Open in new window


Regards.
0
 

Author Comment

by:1jaws
ID: 36589915
thank you for replying, but I am really getting this figure it out based on that code I posted... if this is possible can you paste the working code, cause I am not succesfull on getting it working yet...
0
 
LVL 27

Assisted Solution

by:Shaun Kline
Shaun Kline earned 1000 total points
ID: 36590042
This is the code using the width parameter that is passed into the function.
If this does not work and errors out, provide the error message. If the image does not display correctly, provide a screen capture and explain why it isn't working (i.e. how is the image distorted).
public void CreateThumb(string fileName, System.IO.Stream imageStream, int width, int height)  
{  
    System.Drawing.Image.GetThumbnailImageAbort thumbnailImageAbortDelegate = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);  
    using (Bitmap originalImage = new Bitmap(imageStream))  
    {  
        int newHeight = originalImage.Height * (width / originalImage.Width);
        using (System.Drawing.Image thumbnail = originalImage.GetThumbnailImage(width, newHeight, thumbnailImageAbortDelegate, IntPtr.Zero))  
        {  
            thumbnail.Save(fileName);  
        }  
    }  
}

Open in new window

0
 

Author Comment

by:1jaws
ID: 36590114
 using (System.Drawing.Image thumbnail = originalImage.GetThumbnailImage(width, newHeight, thumbnailImageAbortDelegate, IntPtr.Zero))

this line gives error saying Out of Memory..

when I debugged I see new height = 0
0
 

Author Comment

by:1jaws
ID: 36590190
I no longer need this questions answer... Thanks for all the efforts to all!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
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…

705 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