public static unsafe BitmapBase SizePos(BitmapBase source, double scaleWidth, double scaleHeight, int inX, int inY, int outX, int outY, int maxWidth = 0, int maxHeight = 0, Filter filter = null) { if (source.Width <= 0 || source.Height <= 0) { return(source.ToBitmapSame()); } PixelRect pureImg = source.PreciseSize(0); if (pureImg.Width <= 0 || pureImg.Height <= 0) { return(source.ToBitmapSame()); } int outWidth = (int)Math.Round(pureImg.Width * scaleWidth); int outHeight = (int)Math.Round(pureImg.Height * scaleHeight); if (scaleWidth == 1 && scaleHeight == 1) { //no resize needed if (inX != outX || inY != outY) { BitmapBase result; if (maxWidth == 0 && maxHeight == 0) { result = new BitmapRam(outX - inX + source.Width, outY - inY + source.Height); } else { result = new BitmapRam(Math.Min(outX - inX + source.Width, maxWidth), Math.Min(outY - inY + source.Height, maxHeight)); } result.DrawImage(source, outX - inX, outY - inY); return(result); } else { return(source.ToBitmapSame()); } } if (filter == null) { if (scaleWidth < 1) { filter = new LanczosFilter(); } else { filter = new MitchellFilter(); } } int transparentOffset; if (pureImg.Left != 0 || pureImg.Top != 0) { transparentOffset = pureImg.Left * 4 + pureImg.Top * source.Stride; // Resample looks better if transprent pixels is cropped. Especially if the image is square // Data+DataOffset, pureImg.Width, pureImg.Height instead of Data, Width, Height works like left-top cropping } else { transparentOffset = 0; } BitmapBase afterHorzResample, afterVertResample; // Horizontal resampling if (scaleWidth == 1) { afterHorzResample = source; } else { afterHorzResample = new BitmapRam(outWidth, pureImg.Height); ContributorEntry[] contrib = filter.PrecomputeResample(scaleWidth, pureImg.Width, outWidth); Resample1D(afterHorzResample, source, transparentOffset, contrib, outWidth, pureImg.Height, true); transparentOffset = 0; } // Vertical resampling if (scaleHeight == 1) { afterVertResample = afterHorzResample; } else { afterVertResample = new BitmapRam(outWidth, outHeight); ContributorEntry[] contrib = filter.PrecomputeResample(scaleHeight, pureImg.Height, outHeight); Resample1D(afterVertResample, afterHorzResample, transparentOffset, contrib, outHeight, outWidth, false); } BitmapBase final; //At this point image will be resized and moved to another BitmapBase anyway int drawX = outX - (int)Math.Round((inX - pureImg.Left) * scaleWidth); int drawY = outY - (int)Math.Round((inY - pureImg.Top) * scaleHeight); if (maxWidth == 0 && maxHeight == 0) { final = new BitmapRam(Math.Max(drawX + outWidth, maxWidth), Math.Max(drawY + outHeight, maxHeight)); } else { final = new BitmapRam(Math.Max(drawX + outWidth, maxWidth), Math.Max(drawY + outHeight, maxHeight)); } final.DrawImage(afterVertResample, drawX, drawY); return(final); }