0

更新:わかりました!以下に投稿された回答...質問を整理して、他の人にとってより役立つかもしれません。

ライブラリを使用して、WebBrowser コントロールを使用して html ページからサムネイルを生成するサービスがあります。それは問題なく動作しますが、uri を渡すことができる WCF サービスを作成する必要があり、サービスはそれをサムネイルに変換して返します。Stream と byte[] として試してみました。それはサービスで画像を作成し、それを証明するためだけにファイルとして保存しますが、それを消費すると(送信する以上の)データを取得し、表示可能な画像として保存できません。任意の提案をいただければ幸いです。私は WCF の専門家ではないので、簡単に見つけられる何かを見落としていることを願っています。

これがサービスです。これはコンソール アプリでホストされ、Main() プロシージャが含まれています。

namespace RawImageService
{
    [ServiceContract]
    public interface IImageServer
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/image/?uri={uri}")]
        Stream HtmlToImage(string uri);
   }

     public class Service : IImageServer
    {
        public Stream HtmlToImage(string uri)
        {
            string path = @"D:\Templates\HtmlServiceImage.bmp";

            if( File.Exists(path) )
                File.Delete(path);

            if (string.IsNullOrEmpty(uri))
            {
                return null;
            }
            else
            {
                if ((uri.IndexOf("file:", System.StringComparison.Ordinal) < 0) &&
                    (uri.IndexOf("http", System.StringComparison.Ordinal) < 0))
                    uri = "http://" + uri;

                Thumbnail.Uri = uri;
                try
                {
                    Bitmap bitmap =
                        HtmlToThumbnail.WebsiteThumbnail.GetThumbnail(Thumbnail.Uri, Thumbnail.Width,
                                                                      Thumbnail.Hight, Thumbnail.ThumbWidth,
                                                                      Thumbnail.ThumbHight);

                    using (MemoryStream ms = new MemoryStream())
                    {
                        bitmap.Save(ms, ImageFormat.Jpeg);
                        bitmap.Save(path, ImageFormat.Jpeg);

                        ms.Position = 0;

                        WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";

                    return ms;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return null;
            }
        }

        static void Main(string[] args)
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/";
            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
            host.AddServiceEndpoint(typeof(IImageServer), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
            host.Open();
            Console.WriteLine("Service is running");
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();

        }
     }
  } 

これがサービスを消費する私の試みです:

private static void Main(string[] args)
{
    string uri = string.Concat("http://localhost:8000",
                   string.Format("/image/?uri={0}", "file:///D:/Templates/Test4.htm"));

    tryThis(uri);
}

public static void tryThis(string uri)
{
    HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
    request.Method = "GET";

    // Get response   
    using (WebResponse response = request.GetResponse() as WebResponse)
    {
        using (Stream stream = response.GetResponseStream())
        {
            byte[] buffer = new byte[response.ContentLength];
            MemoryStream ms = new MemoryStream();
            int bytesRead, totalBytesRead = 0;

            do
            {
                bytesRead = stream.Read(buffer, 0, buffer.Length);
                totalBytesRead += bytesRead;

                ms.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);

            string path = @"D:/templates/fs.jpg";
            if (File.Exists(path))
                File.Delete(path);

            var fs = new FileStream(path, FileMode.Create);
            fs.Write(ms.ToArray(), 0, totalBytesRead);
            fs.Flush();
            fs.Close();
        }
    }
}

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

.config ファイルには何も追加されていません。私はそれを調べ始めるつもりですが、私の理解では、アプリ自体ですべてを構成できます。

どちらの方法で機能させるかは気にしません。機能させる必要があるだけです。提案をいただければ幸いです。ありがとう。

4

1 に答える 1

0

Stream メソッドが機能するようになりました。これがコードです。次のようにアクセスできます。

http://localhost:8000/image/?uri=file:///D:/Templates/Test4.htm

これが新しい方法です。上記のコードをこれに置き換えると、機能します。

public Stream HtmlToImage(string uri)
{
    string path = @"D:\Templates\HtmlServiceImage.bmp";

    if( File.Exists(path) )
        File.Delete(path);

    if (string.IsNullOrEmpty(uri))
    {
        return null;
    }
    else
    {
        if ((uri.IndexOf("file:", System.StringComparison.Ordinal) < 0) &&
            (uri.IndexOf("http", System.StringComparison.Ordinal) < 0))
            uri = "http://" + uri;

        Thumbnail.Uri = uri;
        try
        {
            Bitmap bitmap =
                HtmlToThumbnail.WebsiteThumbnail.GetThumbnail(Thumbnail.Uri, Thumbnail.Width,
                                                              Thumbnail.Hight, Thumbnail.ThumbWidth,
                                                              Thumbnail.ThumbHight);
            MemoryStream ms = new MemoryStream();
            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            ms.Position = 0;
            WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
            return ms;
        }
        catch (Exception)
        {
            throw;
        }
        return null;
    }
}
于 2013-11-04T20:14:36.073 に答える