最好的方法来保存图像? [英] Best way to save an image?

查看:132
本文介绍了最好的方法来保存图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net mvc应用程序,允许上传图片。我想知道做什么是最好的方法。

现在我有这个

  HttpPostedFileBase uploadedImg = Session [SessionImgKey] as HttpPostedFileBase; 

if(uploadedImg!= null)
{
string fileName = CreateFile(MyField.Name,uploadedImg);
tableA.ImagePath = String.Concat(ImgFolderPathLoctaion,\\,fileName);



$ b $ p
$ b

这很好,但我想把它移动到我的服务层,我想要在导入一个web.dll到我的服务层项目。

那么我应该使用一个流?或者像图像保存(我认为这可能更适合图像通过paint class不上传图片。

解决方案

您可以从发布的文件中获取图片流,将其转换为图片System.Drawing)然后保存:

$ $ p $ $ $ $ c $ var stream = uploadedImg.InputStream
var buffer = new byte [ stream.Length];
stream.Read(buffer,0,buffer.Length);
//将流转换为图像
Image myImage = Image.FromStream(new MemoryStream(buffer));
myImage.Save(c:\myimage.jpg);


I have an asp.net mvc application that allows images to be uploaded. I am wondering what is the best way to do it.

Right now I have this

HttpPostedFileBase uploadedImg = Session[SessionImgKey] as HttpPostedFileBase;

if (uploadedImg != null)
{
    string fileName = CreateFile(MyField.Name, uploadedImg);
    tableA.ImagePath = String.Concat(ImgFolderPathLoctaion, "\\", fileName);
}

This is fine but I want to move it my service layer and I don't want to have in import a web.dll into my service layer project.

So should I be using a stream? or something like Image Save (I think this might be more geared for images through the paint class not images uploaded.

解决方案

You can get the image stream from the posted file, convert it in an image (System.Drawing) and then save it:

var stream = uploadedImg.InputStream;
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
//convert stream into image
Image myImage = Image.FromStream(new MemoryStream(buffer));
myImage.Save("c:\myimage.jpg");

这篇关于最好的方法来保存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆