简体   繁体   English

在不使用HTML5画布的情况下将图像转换为base64

[英]Convert an image to base64 without using HTML5 Canvas

First, a little background: 首先,有一点背景:

I apologize ahead of time for the long-winded nature of this preface; 对于此序言的冗长性,我提前表示歉意。 however, it may assist in providing an alternate solution that is not specific to the nature of the question. 但是,它可能有助于提供一种并非针对问题性质的替代解决方案。

I have an ASP.NET MVC application that uses embedded WinForm UserControls. 我有一个使用嵌入式WinForm UserControls的ASP.NET MVC应用程序。 These control provide "ink-over" support to TabletPCs through the Microsoft.Ink library. 这些控件通过Microsoft.Ink库为TabletPC提供“墨水覆盖”支持。 They are an unfortunate necessity due to an IE8 corporate standard; 由于IE8企业标准,它们是不幸的必需品。 otherwise, HTML5 Canvas would be the solution. 否则,HTML5 Canvas将是解决方案。

Anyway, an image URL is passed to the InkPicture control through a <PARAM> . 无论如何,图像URL通过<PARAM>传递到InkPicture控件。

<object VIEWASEXT="true" classid="MyInkControl.dll#MyInkControl.MyInkControl" 
        id="myImage"  name="myImage" runat="server">
    <PARAM name="ImageUrl" value="http://some-website/Content/images/myImage.png" />
</object>

The respective property in the UserControl takes that URL, calls a method that performs an HttpWebRequest , and the returned image is placed in the InkPicture . UserControl的相应属性采用该URL,调用执行HttpWebRequest的方法,并将返回的图像放置在InkPicture

public Image DownloadImage(string url)
    {
        Image _tmpImage = null;

        try
        {
            // Open a connection
            HttpWebRequest _HttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            _HttpWebRequest.AllowWriteStreamBuffering = true;

            // use the default credentials
            _HttpWebRequest.Credentials = CredentialCache.DefaultCredentials;

            // Request response:
            System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();

            // Open data stream:
            System.IO.Stream _WebStream = _WebResponse.GetResponseStream();

            // convert webstream to image
            _tmpImage = Image.FromStream(_WebStream);

            // Cleanup
            _WebResponse.Close();
        }
        catch (Exception ex)
        {
            // Error
            throw ex;
        }

        return _tmpImage;
    }

Problem 问题

This works, but there's a lot of overhead in this process that significantly delays my webpage from loading (15 images taking 15 seconds...not ideal). 这行得通,但是在此过程中有很多开销,这极大地延迟了我的网页的加载(15张图像耗时15秒……不理想)。 Doing Image img = new Bitmap(url); 正在做Image img = new Bitmap(url); in the UserControl does not work in this situation because of FileIO Permission issues (Full trust or not, I have been unsuccessful in eliminating that issue). 由于FileIO权限问题(无论是否完全信任,我都无法成功消除该问题),因此UserControl则无法在这种情况下使用。


Initial Solution 初始解决方案

Even though using canvas is not a current option, I decided to test a solution using it. 即使使用canvas不是当前的选择,我还是决定使用它来测试解决方案。 I would load each image in javascript, then use canvas and toDataUrl() to get the base64 data. 我将在javascript中加载每个图像,然后使用canvastoDataUrl()获取base64数据。 Then, instead of passing the URL to the UserControl and have it do all the leg work, I pass the base64 data as a <PARAM> instead. 然后,我没有将URL传递给UserControl并让它完成所有工作,而是将base64数据作为<PARAM>传递。 Then it quickly converts that data back to the image. 然后,它将数据快速转换回图像。

That 15 seconds for 15 images is now less than 3 seconds. 15张图像的15秒现在不到3秒。 Thus began my search for a image->base64 solution that worked in IE7/8. 因此,我开始寻找在IE7 / 8中工作的image-> base64解决方案。


Here are some additional requirements/restrictions: 以下是一些其他要求/限制:

  • The solution cannot have external dependencies (ie $.getImageData). 该解决方案不能具有外部依赖项(即$ .getImageData)。
  • It needs to be 100% encapsulated so it can be portable. 它需要100%封装,以便可以移植。
  • The source and quantity of images are variable, and they must be in URL format (base64 data up front is not an option). 图像的来源和数量是可变的,并且它们必须为URL格式(不能选择使用base64数据)。

I hope I've provided sufficient information and I appreciate any direction you're able to give. 希望我提供了足够的信息,也感谢您能提供的任何指导。

Thanks. 谢谢。

You can use any of the FlashCanvas, fxCanvas or excanvas libraries, which simulate canvas using Flash or VML in old internet explorer versions. 您可以使用任何FlashCanvas,fxCanvas或excanvas库,这些库在旧的Internet Explorer版本中使用Flash或VML模拟画布。 I believe all of these provide the toDataURL method from the Canvas API, allowing you to get an encoded representation of your image. 我相信所有这些都提供了Canvas API中的toDataURL方法,使您能够获取图像的编码表示形式。

After extensive digging around (I'm in the same fix) I believe this is the only solution, short of writing a PHP script that you can send the image to. 经过广泛的挖掘(我使用的是相同的修补程序),我相信这是唯一的解决方案,只需编写可将图像发送到的PHP脚本即可。 The problem with that of course is that there isn't a way to send images to the PHP script unless any of these three conditions is true: 当然,问题在于,除非满足以下三个条件之一,否则无法将图像发送到PHP脚本:

  • The browser supports typed arrays ( Uint8Array ) 浏览器支持类型化数组( Uint8Array
  • The browser supports sendAsBinary 浏览器支持sendAsBinary
  • The image is being uploaded by someone via a form (in which case it can be sent to a PHP script that responds with the base 64 encoding) 某人通过表单将图像上传(在这种情况下,可以将其发送到以base 64编码作为响应的PHP脚本)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM