我是 ASP.net 的新手,想知道从外部域 (Amazon S3) 加载照片是多么容易,使用它们的过期链接,并将照片存储在浏览器内存中以供另一个脚本使用一个 OpenBinary 方法?这允许我在打印到屏幕之前调整大小和水印。
这就是我想要发生的事情:
在 loadImage.aspx 上,我从我的数据库中获取照片 ID,为 Amazon S3 创建一个到期的签名 URL,以某种方式调用照片并将其保存到内存中。在内存中时,我的 ASP.Jpeg 脚本将调用 OpenBinary 方法,调整照片大小并为其添加水印,然后使用 SendBinary 方法显示照片。
我认为内存流或响应二进制写入可能是我正在寻找的东西,但不确定如何在外部照片源上使用它。到目前为止,这是我所做的,但我很困惑,我想我会得到帮助,因为我不确定这是否可行,如果我可以在内存中加载外部域照片,如果我遗漏了一些重要的东西…… ..
我的图像元素:
<img src="loadImage.aspx?p=234dfsdfw5234234">
在 loadImage.aspx 上:
string AWS_filePath = "http://amazon............"
using (FileStream fileStream = File.OpenRead(AWS_filePath))
{
MemoryStream memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
}
// Persits ASP.Jpeg Component
objJpeg.OpenBinary( ... );
// resize bits
// watermark bits
objJpeg.SendBinary( ... );
任何帮助都会很棒。
最佳答案
首先开始使用处理程序 .ashx
而不是完整的 .aspx
页面。处理程序没有 aspx 页面的所有调用,对于您要发送的内容更加清晰,并且您可以避免所有现成的现有 header 。
<img src="loadImage.ashx?p=234dfsdfw5234234">
如何下载图片。
string url = "http://amazon............"
byte[] imageData;
using (WebClient client = new WebClient()) {
imageData = client.DownloadData(url);
}
如何将图片发送到浏览器
// this is the start call from the handler
public void ProcessRequest(HttpContext context)
{
// imageData is the byte we have read from previous
context.Response.OutputStream.Write(imageData, 0, imageData.Length);
}
如何设置缓存和header
public void ProcessRequest(HttpContext context)
{
// this is a header that you can get when you read the image
context.Response.ContentType = "image/jpeg";
// the size of the image
context.Response.AddHeader("Content-Length", imageData.Length.ToString());
// cache the image - 24h example
context.Response.Cache.SetExpires(DateTime.Now.AddHours(24));
context.Response.Cache.SetMaxAge(new TimeSpan(24, 0, 0));
// render direct
context.Response.BufferOutput = false;
...
}
我希望这些提示可以帮助您继续前进。
亲戚:
https://stackoverflow.com/search?q=%5Basp.net%5D+DownloadData
关于c# - 从外部域 (AWS S3) 加载图像并将其存储在浏览器内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11590342/