C# 将位图转换为 ASP.NET 的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1606784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert bitmap to image for ASP.NET
提问by alina
this is how my code look now:
这是我的代码现在的样子:
System.Drawing.Image objImage = System.Drawing.Image.FromFile(Server.MapPath("aaa.jpg"));
int height = objImage.Height;
int width = objImage.Width;
System.Drawing.Bitmap bitmapimage = new System.Drawing.Bitmap(objImage, width, height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmapimage);
System.Drawing.Image bitmap2 = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("sem.png"));
g.DrawImage(bitmap2, (objImage.Width - bitmap2.Width) / 2, (objImage.Height - bitmap2.Height) / 2);
MemoryStream stream = new MemoryStream();
bitmapimage.Save(stream, ImageFormat.Jpeg);
String saveImagePath = Server.MapPath("ImagesMerge/") + "aaa.jpg";
bitmapimage.Save(saveImagePath);
imgBig.ImageUrl = saveImagePath;
The problem I have now is that the image is not displayed in browser, I don't understand why .
我现在遇到的问题是图像没有显示在浏览器中,我不明白为什么 .
回答by Arjan Einbu
Bitmap
is a subclass of Image
, so there no need to convert Bitmap
to Image
. It already is...
Bitmap
是 的子类Image
,因此无需转换Bitmap
为Image
. 已经是...
回答by Fredrik M?rk
Probably because saveImagePath
will be a local path (such as c:\somepath\aaa.jpg
) that is not reachable from the browser. You probably want to set the ImageUrl = "ImagesMerge/aaa.jpg"
instead.
可能是因为saveImagePath
将是c:\somepath\aaa.jpg
浏览器无法访问的本地路径(例如)。您可能想要设置ImageUrl = "ImagesMerge/aaa.jpg"
代替。
回答by Otávio Décio
MapPath will give you a physycaladdress, not a virtual address which is what the browser needs to get to the image.
在MapPath会给你一个physycal地址,而不是虚拟地址是什么浏览器需要获取到的图像。
回答by jerjer
You can also try:
您也可以尝试:
imgBig.ImageUrl = ResolveUrl(saveImagePath);
EDIT:
编辑:
If saveImagePath is under the WebApplication Directory, doing some modifications on the directory structure i.e. modifying files, deleting and creating can cause the application pool to recycle, and once it reaches the maximum recycle count the application pool will be stopped causing "Server unavailable" error.
如果saveImagePath在WebApplication目录下,对目录结构做一些修改,比如修改文件,删除和创建会导致应用程序池回收,一旦达到最大回收计数,应用程序池就会停止,导致“服务器不可用”错误.
I would suggests to add/save/modify images on a separate directory (not under the Apps Directory) then create a Handler(ASHX) that will read the images, just an advice though.
我建议在单独的目录(不在应用程序目录下)添加/保存/修改图像,然后创建一个将读取图像的处理程序(ASHX),不过只是一个建议。
回答by Fredou
like jmaglasangsaid, I would suggest to you to use an ashx file and if you don't need to keep the image, just send the image stream directly to the http without saving it on the disk
就像jmaglasang说的,我建议你使用 ashx 文件,如果你不需要保留图像,只需将图像流直接发送到 http,而不将其保存在磁盘上
so you only need to do something like
所以你只需要做类似的事情
<img src="Handler.ashx?action=merge&image1=blah.jpg&image2=bloh.jpg">
look at this codefor an example of how to send an image made in memory that does not exist on the drive
查看此代码的示例,了解如何发送在驱动器上不存在的内存中制作的图像
回答by azamsharp
You might be forgetting to set the Response.Headers. Check out the following example that shows how to create bar chart images and then display it on the screen:
您可能忘记设置 Response.Headers。查看以下示例,该示例展示了如何创建条形图图像并将其显示在屏幕上:
http://www.highoncoding.com/Articles/399_Creating_Bar_Chart_Using__NET_Graphics_API.aspx
http://www.highoncoding.com/Articles/399_Creating_Bar_Chart_Using__NET_Graphics_API.aspx