Image Resize in C# - Algorith to determine resize dimensions (height and width)

后端 未结 5 1148
挽巷
挽巷 2020-12-28 23:32

I need to scale down an image that has a height or width greater than a predefined pixel value.

I wrote some code that takes a look at the original image, checks to

5条回答
  •  时光取名叫无心
    2020-12-29 00:14

    I did something similar for Bitmaps, but idea is same:

    1. get image height and width
    2. get current screen resolution
    3. calculate aspect ratio (ASR) from image size
    
    Handle following cases:
    
    4. if ASR >=1 and image width > image height
        if image width > screen width {}
            if image height > screen height {}
            else if image width > screen width {}
        else {}
       else
        if image height > screen height {}
        else if image width > screen width {}
        else {}
    

    //SCREEN_SIZE is configurable; Defs.SCREEN_SIZE = 100; // and boolPixelAR is true;

    Try following code:

                // PERCENTAGE OF IMAGE -> TODO: Configurable? IMAZE ZOOM / SCREEN PERCENTAGE
                Double HScale = __bmp.Width;// *Defs.SCREEN_SIZE / 100;
                Double VScale = __bmp.Height;// *Defs.SCREEN_SIZE / 100;
                Double __aspectRatio;
                Double __screenRatio = _currentScreenSize.Width / _currentScreenSize.Height;
    
                // PERCENTAGE OF SCREEN
                if (!_boolPixelAR) {
                    HScale = _currentScreenSize.Width * Defs.SCREEN_SIZE / 100;
                    VScale = _currentScreenSize.Height * Defs.SCREEN_SIZE / 100;
                }
                else {
                    __aspectRatio = HScale / VScale;
                    if( __aspectRatio >= 1)
                        if (HScale >= _currentScreenSize.Width) {  // Long Edge is WIDTH. For 100%, HScale = WIDTH
                            VScale = ((VScale * _currentScreenSize.Width) / HScale) * Defs.SCREEN_SIZE / 100;
                            HScale = _currentScreenSize.Width * Defs.SCREEN_SIZE / 100;
    
                            if (VScale > _currentScreenSize.Height) {                  // Long Edge is HEIGHT. For 100%, VScale = HEIGHT
                                //__aspectRatio = VScale / HScale;
                                HScale = ((HScale * _currentScreenSize.Height) / VScale) * Defs.SCREEN_SIZE / 100;
                                VScale = _currentScreenSize.Height * Defs.SCREEN_SIZE / 100;
                            }
                        }
                        else if (VScale > _currentScreenSize.Height) {                  // Long Edge is HEIGHT. For 100%, VScale = HEIGHT
                            //__aspectRatio = VScale / HScale;
                            HScale = ((HScale * _currentScreenSize.Height) / VScale) * Defs.SCREEN_SIZE / 100;
                            VScale = _currentScreenSize.Height * Defs.SCREEN_SIZE / 100;
                        } 
                        else {
                            //Do nothing... Just set Zoom.
                            HScale = HScale * Defs.SCREEN_SIZE / 100;
                            VScale = VScale * Defs.SCREEN_SIZE / 100;
                        }
                    else 
                        if (VScale > _currentScreenSize.Height) {                  // Long Edge is HEIGHT. For 100%, VScale = HEIGHT
                            //__aspectRatio = VScale / HScale;
                            HScale = ((HScale * _currentScreenSize.Height) / VScale) * Defs.SCREEN_SIZE / 100;
                            VScale = _currentScreenSize.Height * Defs.SCREEN_SIZE / 100;
                        }
                        else if (HScale >= _currentScreenSize.Width) {  // Long Edge is WIDTH. For 100%, HScale = WIDTH
                            VScale = ((VScale * _currentScreenSize.Width) / HScale) * Defs.SCREEN_SIZE / 100;
                            HScale = _currentScreenSize.Width * Defs.SCREEN_SIZE / 100;
                        } 
                        else {
                            //Do nothing... Just set Zoom.
                            HScale = HScale * Defs.SCREEN_SIZE / 100;
                            VScale = VScale * Defs.SCREEN_SIZE / 100;
                        }
    
                    ////__aspectRatio = VScale / HScale;
                    //HScale = ((HScale * _currentScreenSize.Height) / VScale) * Defs.SCREEN_SIZE / 100;
                    //VScale = _currentScreenSize.Height * Defs.SCREEN_SIZE / 100;
                }
    
                Bitmap scaledBmp = GraphicsFactory.ResizeImage(
                                            __bmp,
                                            Convert.ToInt32(HScale),
                                            Convert.ToInt32(VScale));
    

提交回复
热议问题