我在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. 流媒體在應用程序的方法