【发布时间】:2015-08-12 23:14:25
【问题描述】:
我正在使用 asp.net 网络表单和 jcrop(文件上传后裁剪图像)..
这个例子我在这里将临时图像存储到文件系统,但是..由于天蓝色我不能(也不会)这样做..
Soo.. 我怎样才能拥有相同的功能而不保存到磁盘?这意味着......如果可能的话,我希望有一个流在整个线上......
public partial class UserConfig : System.Web.UI.Page
{
String path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
Boolean FileOK = false;
Boolean FileSaved = false;
if (Upload.HasFile)
{
Session["WorkingImage"] = Upload.FileName;
String FileExtension = Path.GetExtension(Session["WorkingImage"].ToString()).ToLower();
String[] allowedExtensions = { ".png", ".jpeg", ".jpg", ".gif" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (FileExtension == allowedExtensions[i])
{
FileOK = true;
}
}
}
if (FileOK)
{
try
{
Upload.PostedFile.SaveAs(path + Session["WorkingImage"]);
FileSaved = true;
}
catch (Exception ex)
{
lblError.Text = "File could not be uploaded." + ex.Message.ToString();
lblError.Visible = true;
FileSaved = false;
}
}
else
{
lblError.Text = "Cannot accept files of this type.";
lblError.Visible = true;
}
if (FileSaved)
{
pnlUpload.Visible = false;
pnlCrop.Visible = true;
imgCrop.ImageUrl = "images/" + Session["WorkingImage"].ToString();
}
}
protected void btnCrop_Click(object sender, EventArgs e)
{
string ImageName = Session["WorkingImage"].ToString();
int w = Convert.ToInt32(W.Value);
int h = Convert.ToInt32(H.Value);
int x = Convert.ToInt32(X.Value);
int y = Convert.ToInt32(Y.Value);
byte[] CropImage = Crop(path + ImageName, w, h, x, y);
using (MemoryStream ms = new MemoryStream(CropImage, 0, CropImage.Length))
{
ms.Write(CropImage, 0, CropImage.Length);
using (SD.Image CroppedImage = SD.Image.FromStream(ms, true))
{
string SaveTo = path + "crop" + ImageName;
CroppedImage.Save(SaveTo, CroppedImage.RawFormat);
pnlCrop.Visible = false;
pnlCropped.Visible = true;
imgCropped.ImageUrl = "images/crop" + ImageName;
}
}
}
static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
try
{
using (SD.Image OriginalImage = SD.Image.FromFile(Img))
{
using (SD.Bitmap bmp = new SD.Bitmap(Width, Height))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, OriginalImage.RawFormat);
return ms.GetBuffer();
}
}
}
}
catch (Exception Ex)
{
throw (Ex);
}
}
}
【问题讨论】:
-
如果你想要一个流,把它放在会话中。您必须小心,因为会话中的太多可能会达到内存屏障并导致应用程序池回收。您可以尝试通过序列化字符串将其序列化为视图状态,但这会随着视图状态膨胀而真正减慢往返速度。
-
是的@Mark .. 尤其是在 Session 上犯了错误,而且之前还有些 ViewState..,所以不太急于再去那趟旅行 :)
-
due to azure这需要更多解释。 Azure 是什么让您不愿意将图像存储在磁盘上? -
这是一个很好的观点@Sam :) 我已经有一段时间没有在使用 Azure 网站时在文件系统上存储数据了。记住我有一个 非常 上次我试图调整这个来工作时很难。所以......也许你知道一些我不知道的关于在使用 Azure 时在文件系统上存储文件的事情?
-
我使用 Azure SDK(通过 NuGet 安装)和 Azure 存储 API(CloudStorageAccount、CloudBlobClient、CloudBlobContainer、CloudBlockBlob)。可从 Azure 网站访问 Azure 存储,上传文件后,即可通过标准 URL 访问该文件。