当不同本地化版本的全局资源文件中含有本地化版本的图像文件时,您可以自定义一个名为 MyLocalImage.ashx 的处理程序文件,基于用户的语言首选项来有条件地进行加载,代码如下所示。
页面中的调用方法:
<asp:Image ID="Image1" runat="server" ImageUrl="~/ MyLocalImage.ashx" />
MyLocalImage.ashx 的处理程序的写法:
public class MyLocalImage : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
context.Response.ContentType = "image/png";
string LanaguageReference = ((ProfileCommon)context.Profile).LanguagePreference;
if (!string.IsNullOrEmpty(LanaguageReference))
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanaguageReference);
}
Bitmap bm = Resources.Litware.LitwareSlogan;
MemoryStream image = new MemoryStream();
bm.Save(image,ImageFormat.Png);
context.Response.BinaryWrite(image.GetBuffer());
}
}
MyLocalImage.ashx 中定义的自定义处理程序类可使用您以前在自定义 InitializeCulture 方法中看到的类似逻辑,在从全局资源文件中检索图像文件以前,初始化当前线程的 CurrentUICulture 设置。您可能疑问为何在页面的基类中已经设置了当前线程的CurrentUICulture,而在这里还要重新设置,那是因为这里的线程与基类中处理的线程不是同一线程。在该自定义处理程序正确初始化了 CurrentUICulture 设置之后,它即可通过 MyResource.resx 的强类型化资源类来访问图像文件。然后,便只需将图像文件的数位编写到 HTTP 响应流。
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




