I'm battling with this for a while now and can't seem to get any results. I'm using methods from this SO QA > How to crop an image using C#?
I don't get any errors :/ The code just runs, but the image does not get cropped.
Code:
string fileNameWitPath = "finename.png";
fileNameWitPath = context.Server.MapPath("~/content/branding/" + context.Request.QueryString["userId"] + "/logo" + "/" + fileNameWitPath)
using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Open))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
//get co-ords
int x1 = Convert.ToInt32(context.Request.QueryString["x1"].Trim());
int y1 = Convert.ToInt32(context.Request.QueryString["y1"].Trim());
int x2 = Convert.ToInt32(context.Request.QueryString["x2"].Trim());
int y2 = Convert.ToInt32(context.Request.QueryString["y2"].Trim());
Bitmap b = new Bitmap(fs);
Bitmap nb = new Bitmap((x2 - x1), (y2 - y1));
Graphics g = Graphics.FromImage(nb);
//g.DrawImage(b, x2, y2);
Rectangle cropRect = new Rectangle(x1, y1, nb.Width, nb.Height);
g.DrawImage(b, new Rectangle(x1, y1, nb.Width, nb.Height), cropRect, GraphicsUnit.Pixel);
Byte[] data;
using (var memoryStream = new MemoryStream())
{
nb.Save(memoryStream, ImageFormat.Png);
data = memoryStream.ToArray();
}
bw.Write(data);
bw.Close();
}
}