我正在尝试显示一些使用c#asp.net从数据库中获取的图像,但是我收到了一些错误。
错误:
' /'中的服务器错误应用
C:\ ASP project \ Odiya_Doctor_Client \ Odiya_Doctor_Client \ ODIYA_Doctor_Admin \ Upload \ Banner \ 2015-07-09_01-50-41-PM_Medical-banner-with-icons.jpg
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.IO.FileNotFoundException:C:\ ASP project \ Odiya_Doctor_Client \ Odiya_Doctor_Client \ ODIYA_Doctor_Admin \ Upload \ Banner \ 2015-07-09_01-50-41-PM_Medical-banner-with-icons.jpg
我正在解释下面的代码。
的Index.aspx:
<img runat="server" id="imgCtrl" src='<%# resizeAndConvertToBase64("/ODIYA_Doctor_Admin/Upload/Banner/" + Convert.ToString(Eval("Bnr_Image")),1920,680) %>' class="ls-bg" />
index.aspx.cs:
protected string resizeAndConvertToBase64(string imageDirectory, int newWidth, int newHeight)
{
Bitmap newImage = new Bitmap(newWidth, newHeight);
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(Server.MapPath(imageDirectory));
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}
MemoryStream ms = new MemoryStream();
newImage.Save(ms, ImageFormat.Gif);
var base64Data = Convert.ToBase64String(ms.ToArray());
return "data:image/gif;base64," + base64Data;
}
实际上我想访问c:\ASP\ODIYA_Doctor_Admin\Upload\Banner
文件夹中存在的一个图像,但这里我的inde.aspx页面位于c:\ASP\Odiya_Doctor_Client\Odiya_Doctor_Client
文件夹中。在错误消息中,路径即将到来C:\ASP project\Odiya_Doctor_Client\Odiya_Doctor_Client\ODIYA_Doctor_Admin\Upload\Banner\2015-07-09_01-50-41-PM_Medical-banner-with-icons.jpg
,并且我希望从此路径中删除\Odiya_Doctor_Client\Odiya_Doctor_Client
。所以请帮我解决这个错误。
答案 0 :(得分:1)
将fileupload控件用于文件位置而不是server.mappath 因为server.mappath总是查看你的解决方案文件夹
private void getPicture()
{
string location = FileUpload1.PostedFile.FileName;
resizeAndConvertToBase64(location, 100,100);
}
protected string resizeAndConvertToBase64(string imageDirectory, int newWidth, int newHeight)
{
Bitmap newImage = new Bitmap(newWidth, newHeight);
System.Drawing.Image srcImage = imageDirectory;
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}
MemoryStream ms = new MemoryStream();
newImage.Save(ms, ImageFormat.Gif);
var base64Data = Convert.ToBase64String(ms.ToArray());
return "data:image/gif;base64," + base64Data;
}
答案 1 :(得分:0)
I would suggest you to do this way:
1. Create a application setting:
<appSettings >
<add key="ImgFolderPath" value="c:\ASP\ODIYA_Doctor_Admin\Upload\Banner"/>
</appSettings>
2. Change your aspx code:
<img runat="server" id="imgCtrl" src='<%# resizeAndConvertToBase64( Convert.ToString(Eval("Bnr_Image")),1920,680) %>' class="ls-bg" />
3. Change your .cs page code:
protected string resizeAndConvertToBase64(string imageName, int newWidth, int newHeight)
{
Bitmap newImage = new Bitmap(newWidth, newHeight);
string path = ConfigurationManager.AppSettings["ImgFolderPath"].ToString();
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(path+"/"+imageName));
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}
MemoryStream ms = new MemoryStream();
newImage.Save(ms, ImageFormat.Gif);
var base64Data = Convert.ToBase64String(ms.ToArray());
return "data:image/gif;base64," + base64Data;
}