public void Scale(int?width, int?height, WICBitmapInterpolationMode mode = WICBitmapInterpolationMode.WICBitmapInterpolationModeNearestNeighbor, WicBitmapScaleOptions options = WicBitmapScaleOptions.Default) { if (!width.HasValue && !height.HasValue) { return; } if (width.HasValue && width.Value <= 0) { throw new ArgumentOutOfRangeException(nameof(width)); } if (height.HasValue && height.Value <= 0) { throw new ArgumentOutOfRangeException(nameof(height)); } int neww; int newh; if (width.HasValue && height.HasValue) { neww = width.Value; newh = height.Value; } else { int w = Width; int h = Height; if (w == 0 || h == 0) { return; } if (width.HasValue) { if ((options & WicBitmapScaleOptions.DownOnly) == WicBitmapScaleOptions.DownOnly) { if (width.Value > w) { return; } } neww = width.Value; newh = width.Value * h / w; } else // height.HasValue { if ((options & WicBitmapScaleOptions.DownOnly) == WicBitmapScaleOptions.DownOnly) { if (height.Value > h) { return; } } newh = height.Value; neww = height.Value * w / h; } } if (neww == 0 || newh == 0) { return; } var clip = WICImagingFactory.CreateBitmapScaler(); clip.Object.Initialize(_comObject.Object, neww, newh, mode).ThrowOnError(); _comObject?.Dispose(); _comObject = clip; }
public void Scale(int boxSize, WICBitmapInterpolationMode mode = WICBitmapInterpolationMode.WICBitmapInterpolationModeNearestNeighbor, WicBitmapScaleOptions options = WicBitmapScaleOptions.Default) { if (boxSize <= 0) { throw new ArgumentOutOfRangeException(nameof(boxSize)); } if (Width > Height) { Scale(boxSize, null, mode, options); return; } Scale(null, boxSize, mode, options); }