积极答复者
为何图片不显示??

问题
-
在图片较小时,channel.Ink_Share_Pic(imageData)可以被完整的传递到其它客户端。包括发送方在内的所有客户端都能正确显示图片。
但当图片较大时,channel.Ink_Share_Pic(imageData)只对发送方起作用,就是说,作为发送方的客户端正确地执行了Ink_Share_Pic(byte[] b)方法,并将图片显示在本机上。但其它客户端根本就没执行Ink_Share_Pic(byte[] b)方法,自然图片也就显示不出来了。好像channel没起作用。
全部回复
-
能不能提供一个完整的可以直接运行的例子,你可以上传到 www.skydrive.com 中的公开 目录下,然后把链接贴在这里。我不是很了解你是如果实现对等通道的。
Sincerely,
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
谢谢Bob Bao!
由于我是自定义的解析服务,所以目前没法向您提供完整可运行的例子。我把主要的代码贴上来,下面是client端的。目前代码主要的问题是,比较小的图片(例如8K)可以传递,但大些的图片(例如32K的)就没法传递。
[ServiceContract(CallbackContract = typeof(Itransmsg))]
public interface Itransmsg
{
[OperationContract(IsOneWay = true)]
void Ink_Share_Pic(byte[] buffer_pic);
}
public interface IChatChannel : Itransmsg, IClientChannel
{
}
public partial class w_jj : Window, Itransmsg
{
public w_jj()
{
InitializeComponent();
}
//在窗体load事件启用对等通道
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Uri meshAddress = new Uri("net.p2p://localhost/ChatServer" + is_tiid);//is_tiid聊天室ID
NetPeerTcpBinding binding = new NetPeerTcpBinding();
binding.Resolver.Mode = PeerResolverMode.Custom;
binding.Resolver.Custom.Address = new EndpointAddress("net.tcp://localhost/ChatServer/" + is_tiid);//is_tiid聊天室ID
NetTcpBinding ntb;
ntb = new NetTcpBinding();
ntb.Security.Mode = SecurityMode.None;
ntb.MaxReceivedMessageSize = 6553600;
binding.Resolver.Custom.Binding = ntb;
binding.Security.Mode = SecurityMode.None;
binding.MaxReceivedMessageSize = 6553600;
InstanceContext context = new InstanceContext(this);
factory = new DuplexChannelFactory<IChatChannel>(context, binding, new EndpointAddress(meshAddress));
channel = factory.CreateChannel();
channel.Open();
}
public void Ink_Share_Pic(byte[] buffer_pic)
{
MemoryStream _ms = new MemoryStream(buffer_pic);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = _ms;
bi.EndInit();
ImageBrush ph_ib = new ImageBrush();
ph_ib.ImageSource = bi;
bg_ip.Background = ph_ib;
}
//在下面的按钮事件中调用ink_share_pic
private void button_sendimage_Click(object sender, RoutedEventArgs e)
{
try
{
//选择本地文件对话框
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "演示图片文件|*.jpg;*.jpeg;*.bmp;*.png";
//单选
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == true)
{
Stream stream_p;
if ((stream_p = openFileDialog.OpenFile()) != null)
{
try
{byte[] imageData = new byte[stream_p .Length];
stream_p.Seek(0, System.IO.SeekOrigin.Begin);
stream_p.Read(imageData, 0, imageData.Length);
channel.Ink_Share_Pic(imageData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
如果您还需要服务端的代码和 app .config,我再共享到skydriver下。
十分感谢!
- 已编辑 buhuang 2011年5月25日 12:18
-
我想确定的是 是否图片被完整的传递到了对方,你可以尝试用校验的方法进行一下校验。MD5校验或者其他。 如果图片没有被成功传递,那么就不是WPF层面的问题,我可以帮你移到其它论坛再看看。
Sincerely,
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
那就应该是网络变成开发方面了,你先看看channel的state情况,是不是属于失败状态。
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
channel state一直是opened,挺正常的。
只所以怀疑是wpf的问题,是因为我这部分代码是参考http://www.microsoft.com/china/MSDN/library/Windev/WindowsVista/PeerToPeer.mspx?mfr=true中的例程写的。
我把例程中的对等解析都改为自定义,运行起来挺正常的。
不同的是,微软的例程是winform的。下面是相关部分代码。
public void SharePicture(Stream stream)
{
Image image = Bitmap.FromStream(stream);
pbView.SizeMode = PictureBoxSizeMode.StretchImage;
pbView.Image = image;
}
private void btnShare_Click(object sender, EventArgs e)
{
MemoryStream stream = new MemoryStream();
Image image = pbView.Image;
image.Save(stream, ImageFormat.Jpeg);
stream.Position = 0;
channel.SharePicture(stream);
}
在我的程序中,也尝试传递 stream而非byte,但无论图片大小,即便是发送方的客户端也不会执行ink_share_pic(stream)