AsterixDisplayAnalyser.GraphicUtilities.ResizeImage C# (CSharp) Метод

ResizeImage() публичный статический Метод

public static ResizeImage ( Image image, Size size, bool preserveAspectRatio = false ) : Image
image Image
size System.Drawing.Size
preserveAspectRatio bool
Результат Image
        public static Image ResizeImage(Image image, Size size, bool preserveAspectRatio = false)
        {
            int newWidth;
            int newHeight;
            if (preserveAspectRatio)
            {
                int originalWidth = image.Width;
                int originalHeight = image.Height;
                float percentWidth = (float)size.Width / (float)originalWidth;
                float percentHeight = (float)size.Height / (float)originalHeight;
                float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
                newWidth = (int)(originalWidth * percent);
                newHeight = (int)(originalHeight * percent);
            }
            else
            {
                newWidth = size.Width;
                newHeight = size.Height;
            }
            Image newImage = new Bitmap(newWidth, newHeight);
            using (Graphics graphicsHandle = Graphics.FromImage(newImage))
            {
                graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
            }
            return newImage;
        }

Usage Example

Пример #1
0
        public static void Build(ref GMapOverlay OverlayOut)
        {
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // Display defined radars
            //
            string FileName   = @"C:\ASTERIX\IMAGES\radar.jpg";
            Image  RadarImage = Image.FromFile(FileName);

            // Get radar display attributes
            DisplayAttributes.DisplayAttributesType RadarDisplayAttribute = DisplayAttributes.GetDisplayAttribute(DisplayAttributes.DisplayItemsType.Radar);
            RadarImage = GraphicUtilities.ResizeImage(RadarImage, new Size(RadarDisplayAttribute.ImageSize.Width, RadarDisplayAttribute.ImageSize.Height), false);

            // Here loop through defined radars and display them on the map
            foreach (SystemAdaptationDataSet.Radar Radar in SystemAdaptationDataSet.RadarDataSet)
            {
                // Image properties
                GMapMarkerImage MyMarkerImage =
                    new GMapMarkerImage(new PointLatLng(Radar.RadarPosition.GetLatLongDecimal().LatitudeDecimal, Radar.RadarPosition.GetLatLongDecimal().LongitudeDecimal), RadarImage);
                MyMarkerImage.ToolTipMode = MarkerTooltipMode.Never;
                System.Drawing.SolidBrush myBrush;

                myBrush = new System.Drawing.SolidBrush(RadarDisplayAttribute.TextColor);

                // Get radar marker image
                WaypointMarker WPT_Marker = new WaypointMarker(new PointLatLng(Radar.RadarPosition.GetLatLongDecimal().LatitudeDecimal, Radar.RadarPosition.GetLatLongDecimal().LongitudeDecimal), Radar.RadarName,
                                                               new Font(RadarDisplayAttribute.TextFont, RadarDisplayAttribute.TextSize, FontStyle.Bold, GraphicsUnit.Pixel), myBrush);

                // Load radar marker and label to overlay
                OverlayOut.Markers.Add(MyMarkerImage);
                OverlayOut.Markers.Add(WPT_Marker);
            }
        }
GraphicUtilities