我在c#中用于通过网络在c#中流视频的类不在Windows通用应用程序中接受。 所以,请有人能告诉我如何创建位图将其转换为byte []通过套接字发送并将其转换回位图。首先,通用应用程序中的c#甚至不识别变量类型的位图。它甚至不承认命名空间system.drawing。感谢在Windows通用应用程序中流媒体图像c#
1
A
回答
0
Base64的为位图
string base64string=""; var fileStream = Base64ToStream(base64string); var memStream = new MemoryStream(); await fileStream.CopyToAsync(memStream); memStream.Position = 0; var bitmap = new BitmapImage(); bitmap.SetSource(memStream.AsRandomAccessStream()); lstBitImages.Insert(0, new PhotoModel(bitmap, "00", objPhoto.PhotoName, objPhoto.PhotoBase64, objPhoto.PhotoType, objPhoto.PhotoLink, ".jpg", "Visible", "Collapsed")); displayphotolst.Add(objPhoto); PhotoList.ItemsSource = lstBitImages;
Base64ToStream(base64string);
public Stream Base64ToStream(string base64String)
{
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream stream2 = new MemoryStream(imageBytes);
return stream2;
}
图像的完整路径为位图
objModel.PhotoId = "0";
var fullPath = string.Format(@"{0}\{1}", destinationFolder.Path, guid + "_" + file.Name);
string base64Image = string.Empty;
await Task.Run(() =>
{
Task.Yield();
var photo = File.ReadAllBytes(fullPath);
base64Image = Convert.ToBase64String(photo);
objModel.PhotoBase64 = base64Image;
});
var fileStream = Base64ToStream(base64Image);
var memStream = new MemoryStream();
await fileStream.CopyToAsync(memStream);
memStream.Position = 0;
var bitmap = new BitmapImage();
bitmap.SetSource(memStream.AsRandomAccessStream());
-2
继文档前。 https://docs.microsoft.com/en-us/windows/uwp/networking/sockets
MediaCapture mediaCapture;
string serviceNameForConnect = "22112";
string hostNameForConnect = "localhost";
NetworkAdapter adapter = null;
StreamSocket clientSocket = null;
private async void StartListener_Click(object sender, RoutedEventArgs e)
{
StreamSocketListener listener = new StreamSocketListener();
listener.ConnectionReceived += OnConnection;
await listener.BindServiceNameAsync(serviceNameForConnect);
}
private async void ConnectSocket_Click(object sender, RoutedEventArgs e)
{
HostName hostName;
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync();
try
{
hostName = new HostName(hostNameForConnect);
}
catch (ArgumentException ex)
{
return;
}
clientSocket = new StreamSocket();
try
{
await clientSocket.ConnectAsync(hostName, serviceNameForConnect);
}
catch (Exception exception)
{
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
}
}
private async void Send_Click(object sender, RoutedEventArgs e)
{
object outValue;
// Create a DataWriter if we did not create one yet. Otherwise use one that is already cached.
DataWriter writer;
if (!CoreApplication.Properties.TryGetValue("clientDataWriter", out outValue))
{
writer = new DataWriter(clientSocket.OutputStream);
CoreApplication.Properties.Add("clientDataWriter", writer);
}
else
{
writer = (DataWriter)outValue;
}
while (true)
{
var memoryStream = new InMemoryRandomAccessStream();
await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), memoryStream);
await Task.Delay(TimeSpan.FromMilliseconds(18.288)); //60 fps
memoryStream.Seek(0);
writer.WriteUInt32((uint)memoryStream.Size);
writer.WriteBuffer(await memoryStream.ReadAsync(new byte[memoryStream.Size].AsBuffer(), (uint)memoryStream.Size, InputStreamOptions.None));
// Write the locally buffered data to the network.
try
{
await writer.StoreAsync();
}
catch (Exception exception)
{
// If this is an unknown status it means that the error if fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
}
}
}
private async void OnConnection(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
await Task.WhenAll(DownloadVideos(args));
}
public async Task DownloadVideos(StreamSocketListenerConnectionReceivedEventArgs args)
{
DataReader reader = new DataReader(args.Socket.InputStream);
try
{
while (true)
{
// Read first 4 bytes (length of the subsequent string).
uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));
if (sizeFieldCount != sizeof(uint))
{
// The underlying socket was closed before we were able to read the whole data.
return;
}
uint stringLength = reader.ReadUInt32();
uint actualStringLength = await reader.LoadAsync(stringLength);
if (stringLength != actualStringLength)
{
// The underlying socket was closed before we were able to read the whole data.
return;
}
NotifyUserFromAsyncThread(reader.ReadBuffer(actualStringLength));
}
}
catch (Exception exception)
{
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
}
}
private void NotifyUserFromAsyncThread(IBuffer buffer)
{
var ignore = Dispatcher.RunAsync(
CoreDispatcherPriority.Normal,() =>
{
Stream stream = buffer.AsStream();
byte[] imageBytes = new byte[stream.Length];
stream.ReadAsync(imageBytes, 0, imageBytes.Length);
InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
ms.AsStreamForWrite().Write(imageBytes, 0, imageBytes.Length);
ms.Seek(0);
var image = new BitmapImage();
image.SetSource(ms);
ImageSource src = image;
imageElement.Source = src;
});
}
相关问题
- 1. 顺利流媒体+在Windows 8应用程序上一起Playready
- 2. Flink流媒体 - 在Windows应用功能
- 3. Android音乐流媒体应用程序
- 4. Houseparty应用程序流媒体技术
- 5. 通过应用程序畅通流媒体
- 6. 如何为Windows 8制作流媒体应用程序?
- 7. C# - 通用的Windows应用程序 - 图像源不起作用
- 8. C - FFmpeg流媒体从C程序?
- 9. 保护网络应用程序中的图像媒体
- 10. 在Android应用程序中播放RTSP流媒体
- 11. 如何在流媒体应用程序中登录foreachRDD?
- 12. 在saprk流媒体应用程序中的异常处理
- 13. 在Windows应用程序中的图像滑块应用程序
- 14. 流媒体图像文物
- 15. Windows窗体应用程序的C#图像处理
- 16. 在Windows 8应用程序中使用C#读取Base64图像
- 17. 用nginx流媒体(流畅流媒体)
- 18. C#屏幕流媒体程序
- 19. 媒体播放器流媒体,当退出应用程序音乐应停止
- 20. 在Windows 10通用应用程序中重新着色图像C#
- 21. 使用C#Windows窗体应用程序
- 22. MP3流媒体应用
- 23. C#在Windows窗体应用程序中实时绘图
- 24. 将SQLite窗体应用程序迁移到通用Windows应用程序(C#)
- 25. 如何在C/C++应用程序中使用Windows窗体?
- 26. 用C#中的通用Windows应用程序进行图像编辑#
- 27. 通用Windows的C#:媒体捕获网络摄像头直播
- 28. 在Windows窗体应用程序中使用OpenGL在Visual C++中
- 29. 如何消除流媒体应用程序中的延迟
- 30. 流媒体在应用程序的方法