C# Can';t使用来自WCF的字节[]或流来创建映像
更新:明白了!答案贴在下面。。。我会澄清这个问题,以便对其他人更有用 我有一个服务,它使用一个库,使用WebBrowser控件从html页面生成缩略图。这很好,但我需要创建一个WCF服务,允许您传入uri,然后该服务将其转换为缩略图并返回。我试过将其作为一个流和一个字节[]。它在服务中创建图像,我将其保存为文件只是为了证明它,但当我使用它时,我会得到数据(比我发送的多),而我无法将其保存为可视图像。如有任何建议,将不胜感激。我不是WCF专家,所以我希望我错过了一些容易发现的东西 这是服务,它托管在控制台应用程序中,包含Main()过程C# Can';t使用来自WCF的字节[]或流来创建映像,c#,.net,wcf,steam,C#,.net,Wcf,Steam,更新:明白了!答案贴在下面。。。我会澄清这个问题,以便对其他人更有用 我有一个服务,它使用一个库,使用WebBrowser控件从html页面生成缩略图。这很好,但我需要创建一个WCF服务,允许您传入uri,然后该服务将其转换为缩略图并返回。我试过将其作为一个流和一个字节[]。它在服务中创建图像,我将其保存为文件只是为了证明它,但当我使用它时,我会得到数据(比我发送的多),而我无法将其保存为可视图像。如有任何建议,将不胜感激。我不是WCF专家,所以我希望我错过了一些容易发现的东西 这是服务,它托管
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();
}
}
}
.config文件中没有其他内容。我将开始研究这个问题,但我的理解是,我可以配置应用程序本身的所有内容
我不在乎用哪种方式工作,我只需要让它工作。我感谢你的建议。谢谢。我让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;
}
}
公共流HtmlToImage(字符串uri)
{
字符串路径=@“D:\Templates\HtmlServiceImage.bmp”;
if(File.Exists(path))
删除(路径);
if(string.IsNullOrEmpty(uri))
{
返回null;
}
其他的
{
if((uri.IndexOf(“文件:”,System.StringComparison.Ordinal)<0)&&
(uri.IndexOf(“http”,System.StringComparison.Ordinal)<0))
uri=“http://”+uri;
Uri=Uri;
尝试
{
位图=
HtmlToThumbnail.WebsiteThumbnail.GetThumbnail(Thumbnail.Uri、Thumbnail.Width、,
缩略图。高,缩略图。拇指宽度,
拇指高度);
MemoryStream ms=新的MemoryStream();
保存(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position=0;
WebOperationContext.Current.OutgoingResponse.ContentType=“图像/jpeg”;
返回ms;
}
捕获(例外)
{
投掷;
}
返回null;
}
}
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;
}
}