gpt4 book ai didi

c# - 返回变量作为图像时出错

转载 作者:太空宇宙 更新时间:2023-11-03 23:07:35 28 4
gpt4 key购买 nike

此函数返回调整大小和居中的图像。我想像 thumb.aspx?image=test.jpg&width=100&height=50&needToFill=tru‌ e 一样执行它以获得 ContentType = "image/jpeg"/p>

但是我在 return bmPhoto 时遇到错误 Page_Load(object, System.EventArgs) is null

Note

我知道这段代码是错误的,但我想了解如何在缓冲区中执行相同的功能。

<%@ 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 方法返回值:

void Page_Load(Object sender, EventArgs e) 

您只需要回复即可。删除 return 语句并将方法更改为:

void Page_Load(Object sender, EventArgs e) 
{
//...
var bytes= (byte[])new ImageConverter().ConvertTo(bmPhoto, typeof(byte[]));
Response.ContentType = "image/bmp";
Response.OutputStream.Write(bytes, 0, bytes.Length);
}

关于c# - 返回变量作为图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40654599/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com