Dynamic Image Resize and Data Type issues

Discussion in 'ASP .Net' started by webonomic, Jan 26, 2006.

  1. webonomic

    webonomic Guest

    Converting data types

    I'm trying to do some image manipulation. This code project article
    (http://www.codeproject.com/csharp/imageresize.asp) has a great method
    I want to modefy. Here it is:

    static Image ScaleByPercent(Image imgPhoto, int Percent)
    {
    float nPercent = ((float)Percent/100);

    int sourceWidth = imgPhoto.Width;
    int sourceHeight = imgPhoto.Height;
    int sourceX = 0;
    int sourceY = 0;

    int destX = 0;
    int destY = 0;
    int destWidth = (int)(sourceWidth * nPercent);
    int destHeight = (int)(sourceHeight * nPercent);

    Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
    PixelFormat.Format24bppRgb);
    bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
    imgPhoto.VerticalResolution);

    Graphics grPhoto = Graphics.FromImage(bmPhoto);
    grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

    grPhoto.DrawImage(imgPhoto,
    new Rectangle(destX,destY,destWidth,destHeight),
    new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
    GraphicsUnit.Pixel);

    grPhoto.Dispose();
    return bmPhoto;
    }

    However, I have some business rules for the source image size.
    - height or width cannot be less than 100px, display error in
    <asp:label> to user
    - height or width cannot exceed 1000px, display error in <asp:label> to
    user

    The dest image has rules too:
    - if source image has height > width, set dest height = 100px,
    proportionally set the dest width
    - if source image has width > height, set dest width = 100px,
    proporionally set the dest height
    - if source height = source width, set dest height and width both to
    100px

    In the case of destHeight:
    - if sourceWidth > sourceHeight;
    - destWidth = 100px;
    - destHeight = ((destWidth / sourceWidth) * 100)

    My issues are:
    1) I am having data type issues with getting destHeight or destWidth in
    this manner.
    2) I don't know the proper way to setup the business rules in this
    method. If statement? Switch statment?

    Here is my method so far:

    static System.Drawing.Image ScaleByPercent(System.Drawing.Image
    imgPhoto)
    {


    //float nPercent = ((float)percent / 100);
    int percent;

    int sourceWidth = imgPhoto.Width;
    int sourceHeight = imgPhoto.Height;
    int sourceX = 0;
    int sourceY = 0;

    int destX = 0;
    int destY = 0;
    int destWidth = 0; //(int)(sourceWidth * nPercent);
    int destHeight = 0; //(int)(sourceHeight * nPercent);


    string theMessage = "";


    if (sourceHeight > 100 || sourceWidth > 100)
    {
    if (sourceHeight < 1000 || sourceWidth < 1000)
    {
    if (sourceHeight > sourceWidth)
    {
    destHeight = 100;
    //what do I do here ??
    percent = (float)((destHeight / sourceHeight) *
    100);
    destWidth = (int)(sourceWidth * percent);
    }
    else if (sourceWidth > sourceHeight)
    {
    destWidth = 100;
    //what do I do here ??
    percent = (float)((destWidth / sourceWidth) * 100);
    destHeight = (int)(sourceHeight * percent);
    }
    else if (sourceWidth == sourceHeight)
    {
    destHeight = 100;
    destWidth = 100;
    }
    else
    {
    theMessage = "Image was both greater than 100 and
    less than 1000, something else is wrong";

    }

    theMessage = "Image must be less than 1000 pixels";

    }

    theMessage = "Image must be more than 100 pixels";

    }

    Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
    PixelFormat.Format24bppRgb);
    bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
    imgPhoto.VerticalResolution);

    Graphics grPhoto = Graphics.FromImage(bmPhoto);
    grPhoto.InterpolationMode =
    InterpolationMode.HighQualityBicubic;

    grPhoto.DrawImage(imgPhoto,
    new Rectangle(destX, destY, destWidth, destHeight),
    new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
    GraphicsUnit.Pixel);

    grPhoto.Dispose();
    return bmPhoto;

    }
     
    webonomic, Jan 26, 2006
    #1
    1. Advertising

  2. webonomic

    webonomic Guest

    Well, I'm starting to answer my own question. For the data types, I
    did the following to test things out:

    int a = 101;
    int b = 999;

    int z = 100;
    double c = (a / b)*z;
    lbl.Text = c.ToString();


    double d = ((Convert.ToDouble(a)) / Convert.ToDouble(b))*
    Convert.ToDouble(z);
    lbl2.Text = d.ToString();

    int y = Convert.ToInt32(d);
    lbl3.Text = y.ToString();

    d = Math.Round(d);
    lbl4.Text = d.ToString();

    Now I know how to get my destWidth or destHeight int's.

    What I still would like to know is how I should setup all of my
    business rules. If statements or a switch?
     
    webonomic, Jan 26, 2006
    #2
    1. Advertising

  3. webonomic

    webonomic Guest

    I'll answer my own question even more. For some reason I thought Case
    statements could handle expressions when they do not. Expressions like
    (x > y). So I have to use the If statements.

    I guess all is good. It's a matter of playing around with the code now.



    webonomic wrote:
    > Well, I'm starting to answer my own question. For the data types, I
    > did the following to test things out:
    >
    > int a = 101;
    > int b = 999;
    >
    > int z = 100;
    > double c = (a / b)*z;
    > lbl.Text = c.ToString();
    >
    >
    > double d = ((Convert.ToDouble(a)) / Convert.ToDouble(b))*
    > Convert.ToDouble(z);
    > lbl2.Text = d.ToString();
    >
    > int y = Convert.ToInt32(d);
    > lbl3.Text = y.ToString();
    >
    > d = Math.Round(d);
    > lbl4.Text = d.ToString();
    >
    > Now I know how to get my destWidth or destHeight int's.
    >
    > What I still would like to know is how I should setup all of my
    > business rules. If statements or a switch?
     
    webonomic, Jan 26, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Um9ic29uIENhcnZhbGhvIE1hY2hhZG8=?=

    How to resize an image from SQL IMAGE field

    =?Utf-8?B?Um9ic29uIENhcnZhbGhvIE1hY2hhZG8=?=, Sep 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,466
    Hermit Dave
    Sep 10, 2004
  2. KiwiBrian

    Dynamic resize of image paradox

    KiwiBrian, May 12, 2006, in forum: HTML
    Replies:
    4
    Views:
    583
    sunil
    May 14, 2006
  3. Replies:
    2
    Views:
    1,269
    Adrienne
    Feb 13, 2005
  4. Ranieri Teixeira
    Replies:
    2
    Views:
    153
    Ranieri Teixeira
    Feb 9, 2008
  5. Replies:
    1
    Views:
    134
    PDannyD
    Apr 11, 2005
Loading...

Share This Page