Har bara gjort i c# men...
Jag hackar in lite kod här, det är inte komplett då min imagemanager är ganska gross nuförtiden men detta borde få dig på rätt spår. jag har tagit bort lite som rör min vattenmärkning etc. Observera att du får skicka in bilden som en stream och att du får tillbaka den som bytearray, så du får kanske köra en handler eller nåt som presenterar den... jag använder den i detta fallet för att spara ned bilden i en db och sedan har jag en handler som presenterar den :)
public const int DEFAULT_MAX_LENGTH = 1000;
public const int DEFAULT_QUALITY = 80;
public byte[] ResizeAndWaterMarkPicture(Stream stream)
{
int maxLength;
if (!Int32.TryParse(ConfigurationManager.AppSettings["PictureLongestSide"], out maxLength))
{
maxLength = DEFAULT_MAX_LENGTH;
}
int picQuality;
if (!Int32.TryParse(ConfigurationManager.AppSettings["PictureQuality"], out picQuality))
{
picQuality = DEFAULT_QUALITY;
}
Bitmap bitmap = null;
bitmap = Resize(Image.FromStream(stream), maxLength);
return bitmap;
}
private static Bitmap Resize(Image image, int maxLength)
{
Size size = CalculateNewSizeByLongestSide(maxLength, image.Height, image.Width);
Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(image,
new Rectangle(0, 0, size.Width, size.Height),
new Rectangle(0, 0, image.Width, image.Height),
GraphicsUnit.Pixel);
}
return bitmap;
}
public static Size CalculateNewSizeByLongestSide(int maxLength, int orgHeight, int orgWidth)
{
Size newSize = new Size();
double ratio;
if (orgHeight > orgWidth)
{
newSize.Height = maxLength;
ratio = (double)orgHeight / maxLength;
newSize.Width = (int)Math.Floor(orgWidth / ratio);
}
else
{
newSize.Width = maxLength;
ratio = (double)orgWidth / maxLength;
newSize.Height = (int)Math.Floor(orgHeight / ratio);
}
return newSize;
}
