CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2012
    Posts
    4

    Exclamation C# Solution File - Image Stretches When I Change Color

    Hi everyone.

    I have a UI Design that I got coded by my software developer and

    when I change the color of an image and then run under "debug"

    The image stretches even though I never changed any dimentions or anything.

    All I did was change the color.Any ideas what might be causing this?

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: C# Solution File - Image Stretches When I Change Color

    That's odd - it seems as if you changed the Font more than the color.
    But how did you chaged the color? you meant the background color of a picturebox, or you redrawn the image? If the last one, you should ensure you did it correctly.

    If instead it was the background color of a picturebox, I never heard of that problem, but you could give this a try, set the autoscalemode of form to "none" and try again. If the stretch do not apply anymore then something of weird is happening between colors and fonts...
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Mar 2012
    Posts
    4

    Re: C# Solution File - Image Stretches When I Change Color

    he made a .jpg a button and when i open the .jpg in photoshop and simply change the color and re-save it. and run debug to test out the UI the .jpg button image is stretched.

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: C# Solution File - Image Stretches When I Change Color

    seems as if photoshop changed the size of jpg too. Could you save them to disk and then see the size in bytes or the properties to see if the chaged with and height?
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Mar 2012
    Posts
    4

    Re: C# Solution File - Image Stretches When I Change Color

    it seems the height and width stay the same but when I change the color the size in bytes increases.

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: C# Solution File - Image Stretches When I Change Color

    Take a copy of the original, put a single point of color in the centre and save it as is, without changin the rest of it. Now use it as the image in your program. Is it stretched or is fine?
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Mar 2012
    Posts
    4

    Re: C# Solution File - Image Stretches When I Change Color

    yes. i put a tiny dot and the original picture jumped from 10 kilobytes to 13kilobytes and then i ran the debugger and the picture enlarged.

    Do you think it has something to do with the size of the file?

  8. #8
    Join Date
    Jan 2010
    Posts
    1,133

    Re: C# Solution File - Image Stretches When I Change Color

    When you say that the image "stretched" - do you mean that it rescaled proportionately (became bigger or smaller)?

    The problem is probably related to image DPI resolution - the Graphics class uses this value to rescale the image for the final display with respect to the selected values for PageScale and PageUnit properties.
    You can do something like this in code:
    Code:
    float oldDpiX = bmp.HorizontalResolution;
    float oldDpiY = bmp.VerticalResolution;
    bmp.SetResolution(g.DpiX, g.DpiY);
    g.DrawImage(bmp, buttonLoc.X, buttonLoc.Y);     // EDIT: g is a Graphics instance
    
    // sometime later...
    bmp.SetResolution(oldDpiX, oldDpiY);
    Or you can adjust the resolution in Photoshop, to match the one of the original file.
    Go to Image menu > Image Size... and set the Resolution field, then save. (You can check the resolution of the original image the same way.)

    P.S. Make sure it says "pixels/inch" in the dropdown menu.

  9. #9
    Join Date
    Jan 2010
    Posts
    1,133

    Re: C# Solution File - Image Stretches When I Change Color

    Just done a little experiment; this should also do the trick, in a neater way, if you're up for some code editing:
    g.DrawImage(bmp, targetRectangle, srcRectangle, GraphicsUnit.Pixel);

    The Graphics class uses pixels as PageUnit on displays, but uses "points" (1/72 in) when sampling images by default. The method overload used above allows you to specify that pixels should be used instead.

  10. #10
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: C# Solution File - Image Stretches When I Change Color

    by the way: what I think is photoshop is changing the image enought to make it not suitable for your software. You could see if you have some options under the SaveAs of photoshop - like preserving quality or even changing the type form jpg to Png.
    This last format should be better changeable than jpg
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured