Эта функция возвращает изображение с уменьшенным и центрированным изображением. Я хотел бы ту выполнить его как thumb.aspx? Изображение = test.jpg & ширина = 100 высота = needToFill = Tru е получить ContentType = "изображение/JPEG"Ошибка в возвращаемой переменной как изображение
Но я получить ошибку на возвращения bmPhotoPage_Load(object, System.EventArgs) is null
Примечание
Я знал, что этот код неверен, но я хотел бы понять, как выполнять ту же функцию в буфере.
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e) {
Response.Cache.VaryByParams["Image;Width;Height;needToFill"] = true;
Response.ContentType = "image/jpeg";
string imageLocation = Server.MapPath(Request.QueryString["Image"]);
int Width = Convert.ToInt32(Request.QueryString["Width"]);
int Height = Convert.ToInt32(Request.QueryString["Height"]);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageLocation);
int sourceWidth = image.Width;
int sourceHeight = image.Height;
int sourceX = 0;
int sourceY = 0;
double destX = 0;
double destY = 0;
double nScale = 0;
double nScaleW = 0;
double nScaleH = 0;
bool needToFill=true;
nScaleW = ((double)Width/(double)sourceWidth);
nScaleH = ((double)Height/(double)sourceHeight);
if (Request.QueryString["needToFill"] != null) {
needToFill = Convert.ToBoolean(Request.QueryString["needToFill"]);
}
if (!needToFill) {
nScale = Math.Min(nScaleH, nScaleW);
} else {
nScale = Math.Max(nScaleH, nScaleW);
destY = (Height - sourceHeight * nScale)/2;
destX = (Width - sourceWidth * nScale)/2;
}
if (nScale > 1) nScale = 1;
int destWidth = (int)Math.Round(sourceWidth * nScale);
int destHeight = (int)Math.Round(sourceHeight * nScale);
System.Drawing.Bitmap bmPhoto = null;
try {
bmPhoto = new System.Drawing.Bitmap(destWidth + (int)Math.Round(2 * destX), destHeight + (int)Math.Round(2 * destY));
}
catch (Exception ex) {
throw new ApplicationException(string.Format("destWidth:{0}, destX:{1}, destHeight:{2}, desxtY:{3}, Width:{4}, Height:{5}",
destWidth, destX, destHeight, destY, Width, Height), ex);
}
using (System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto)) {
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.CompositingQuality = CompositingQuality.HighQuality;
grPhoto.SmoothingMode = SmoothingMode.HighQuality;
Rectangle to = new System.Drawing.Rectangle((int)Math.Round(destX), (int)Math.Round(destY), destWidth, destHeight);
Rectangle from = new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight);
grPhoto.DrawImage(image, to, from, System.Drawing.GraphicsUnit.Pixel);
return bmPhoto;
}
}
</script>
Вы не можете вернуть что-то по типу типа void. Вы пытались записать изображение в поток ответов? –
нет, потому что я не .net dev. Поэтому я действительно не знаю, как двигаться. @JuergenGutsch – Vixed
https://blogs.msdn.microsoft.com/laurelle/2011/11/15/writing-a-generic-asp-net-handler-to-return-an-image-and-control-the-image/ –