0

I'm struggling to figure out how to load an image from an embedded resource using a custom scheme handler and cefsharp. I am currently using it to load html, css and js files with no issue however when attempting to load an image i always get the broken image indicator. (Dev tools also fails to show a response) I found that the default example in cefsharp.example for ResourceHandler does not properly handle anything over 32KB, so i made a small change so that it wouldn't clip files over 32KB, but either way i still get broken images for any image i attempt to serve as an embedded resource.

Below is the ProcessRequest method required by IResourceHandler.

public bool ProcessRequest(IRequest request, ICallback callback)
        {
            // The 'host' portion is entirely ignored by this scheme handler.
            //Replace internal with our assembly namespacing:
            var url = request.Url.Replace("internal://", "MyAssembly.Html.").TrimEnd('/');

            fileName = url.Replace('/', '.');
            byte[] bytes = null;

            var assembly = Assembly.GetExecutingAssembly();
            using (Stream embeddedFileStream = assembly.GetManifestResourceStream(fileName))
            {
                if (embeddedFileStream != null)
                {
                    using (StreamReader reader = new StreamReader(embeddedFileStream))
                    {
                        bytes = Encoding.UTF8.GetBytes(reader.ReadToEnd());
                    }
                }
            }

            if (bytes == null)
            {
                callback.Dispose();
            }
            else
            {
                Task.Run(() =>
                {
                    using (callback)
                    {
                        stream = new MemoryStream(bytes);
                        var fileExtension = Path.GetExtension(fileName);
                        mimeType = ResourceHandler.GetMimeType(fileExtension);

                        callback.Continue();
                    }
                });

                return true;
            }

            return false;
        }

As mentioned, this works for html,js,css files but not any type of image. I'm lost at this point as to whats missing. Any help would be greatly appreciated.

b0redom
  • 387
  • 2
  • 11
  • Make sure your mimetype is correct, won't load otherwise. Data is buffered in 32kb chunks, the example has at least three files that are larger and load correctly – amaitland Mar 06 '17 at 04:53
  • The mimetype is correct, comes back as image/jpeg for my jpg. I put a breakpoint in ReadResponse, it never called the method more than once per file (Even saw clipped css files in dev tools) – b0redom Mar 07 '17 at 14:07
  • I added a couple of hundred KB image to the example project and it loads just fine. Make sure your using the latest version which is 55.0.0. – amaitland Mar 07 '17 at 19:41
  • Are you extending RequestHandler, or implementing the interface (like the CefSchemeHandler) – b0redom Mar 08 '17 at 13:53
  • Neither, use one of the static ResourceHandler methods – amaitland Mar 08 '17 at 22:22

0 Answers0