想实现ASP.NET实现在线截图的功能,需要使用两部分代码,请注意查看相应文章。
这里是ASP.NET,JS代码在上篇。
二、ASP.NET的代码
string left = txt_left.Text;
string top = txt_top.Text;
string fileName = "";
string Extension = "";
int width=0;
int height=0;
DirectoryInfo path = new DirectoryInfo(Server.MapPath("~/Images"));
foreach (FileInfo file in path.GetFiles())
{
if (file.Name.Substring(0,file.Name.IndexOf("."))=="Chrysanthemum")
{
Extension = file.Extension;
fileName = Guid.NewGuid().ToString() + file.Extension;
File.Copy(Server.MapPath("~/Images") + "/" + file.Name, Server.MapPath("~/Images") + "/" + fileName);
}
}
if (fileName!="")
{
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("~/Images") + "/" + fileName);
width=image.Width;
height=image.Height;
int newWidth = 240 * width / (240 + 155);
int newHeight = 180 * height / (180 + 115);
int x = Convert.ToInt32(left) * width / (240 + 155);
int y = Convert.ToInt32(top) * height / (180 + 115);
Bitmap bmp = new Bitmap(newWidth, newHeight);
Graphics graphics = Graphics.FromImage(bmp);
Point[] destParal = new Point[] { new Point() { X = 0, Y = 0 }, new Point() { X = newWidth, Y = 0 }, new Point() { X = 0, Y = newHeight } };
graphics.DrawImage(image, destParal, new Rectangle() { X = x, Y = y, Width = newWidth, Height = newHeight }, GraphicsUnit.Pixel);
bmp.Save(Server.MapPath("~/Images")+"/"+ Guid.NewGuid().ToString() + Extension);
graphics.Dispose();
image.Dispose();
File.Delete(Server.MapPath("~/Images") + "/" + fileName);
}