I am receiving H264 encoded frame but when I convert it into bitmap I just get a black screen. The resolution is right. I have tried a lot of things and couldnt find a working way. Thank you!
Here is my code
public System.Drawing.Bitmap CopyDataToBitmap(byte[] data)
{
//Here create the Bitmap to the know height, width and format
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap((int)2592, (int)1936, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//Create a BitmapData and Lock all pixels to be written
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(
new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
//Copy the data from the byte array into BitmapData.Scan0
Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
//Unlock the pixels
bmp.UnlockBits(bmpData);
//Return the bitmap
return bmp;
}
public async void ListenVideo()
{
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 11111);
UdpClient newsock = new UdpClient(ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 11111);
data = newsock.Receive(ref sender);
string message = Encoding.UTF8.GetString(data, 0, data.Length);
while (true)
{
data = newsock.Receive(ref sender);
message = Encoding.UTF8.GetString(data, 0, data.Length);
MemoryStream stream = new MemoryStream(data);
panel1.BackgroundImage = CopyDataToBitmap(data);
await Task.Delay(2000);
}
}
H.264 is encoding for elementary video stream, it is not an encoding for a separate image.
This means that decoding process involves a setup for video encoding. There is no single function (at least it is not supposed to work this way) to take bitstream and output a video frame.
You would typically setup stream decoding context, then feed input and pull output when it is ready.
Windows comes with a stock H.264 encoder and a typical API to consume such decoding services is Media Foundation. It is not the API for .NET so hence Media Foundation with C#
There is also Media Foundation .NET project with C# wrappers over native Media Foundation API.
Related
We have an Aiwit camera that connects to our wifi. I would like to connect to it (in this case using C#) and retrieve data from the camera.
Anyone who was experience with these cameras that can help me out. Also with determining the camera's portnumber.
Some code I'm working with rn:
TcpClient client = new TcpClient("Camera IP", Camera Port);
NetworkStream stream = client.GetStream();
// Request an image
byte[] request = Encoding.ASCII.GetBytes("GET /image.jpg HTTP/1.1\r\n\r\n");
stream.Write(request, 0, request.Length);
// Read the image
byte[] imageData = new byte[1024*1024]; // buffer to store image data
int bytesRead = stream.Read(imageData, 0, imageData.Length);
// Display the image
Image image = Image.FromStream(new MemoryStream(imageData, 0, bytesRead));
I am trying print from Bluetooth printer (INTERMEC PB51), using Xamarin native android.
I have written the code based on the below link.
How can I print an image on a Bluetooth printer in Android?
My code is below.
private static byte[] SELECT_BIT_IMAGE_MODE = { 0x1B, 0x2A, 33, (byte)255, 0 };
Making bitmap as below.
Bitmap sigImage = BitmapFactory.DecodeResource(Resources, Resource.Drawable.icn_logo_jpg);
Creating Blutooth Socket.
BluetoothSocket socket = null;
BufferedReader inReader = null;
BufferedWriter outReader = null;
string bt_printer = address; //AdminSettings.PrinterMACAddr;
if (string.IsNullOrEmpty(bt_printer)) bt_printer = "00:13:7B:49:D1:8C";
BluetoothDevice mmDevice = BluetoothAdapter.DefaultAdapter.GetRemoteDevice(bt_printer);
UUID applicationUUID = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");
socket = mmDevice.CreateRfcommSocketToServiceRecord(applicationUUID);
socket.Connect();
Calling method
PrintImage(bitMap, socket);
Created method for printing it
public void PrintImage(Bitmap bitmap, BluetoothSocket _socket)
{
try
{
if (!_socket.IsConnected)
{
_socket.Connect();
}
MemoryStream stream = new MemoryStream();
//IMAGE
byte[] imageData = ImageToByte2(bitmap);
stream.Write(imageData, 0, imageData.Length);
stream.Write(SELECT_BIT_IMAGE_MODE, 0, SELECT_BIT_IMAGE_MODE.Length);
var bytes = stream.ToArray();
_socket.OutputStream.Write(bytes, 0, bytes.Length);
// Java.Lang.Thread.Sleep(2000);
//END IMAGE
Java.Lang.Thread.Sleep(2000);
}
catch (Exception ex)
{
throw new Exception("Unable to print. Please re-configure the printer and try again!");
}
}
public static byte[] ImageToByte2(Bitmap bitmap)
{
MemoryStream stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
byte[] bitmapData = stream.ToArray();
return bitmapData;
}
But getting logo printed as below image.
The Intermec PB51 can be setup with several different printer languages; IPL, Fingerprint, Direct Protocol, ZSim, DSim, CSim and ESC/P. So first you have to know which printer language you are dealing with. { 0x1B, 0x2A, 33, (byte)255, 0 } is a commnd in ESC/P, so the printer must be in ESC/P mode.
Looks like you are sending PNG image data to the printer. I have only used the Intermec PB51 in ESC/P mode, and in ESC/P the image has to be converted to a 1-bit image byte array (one bit per printer "pixel").
I am sending an image(a screenshot, particularly) via a tcpclient/networkstream. To read the image bytes properly on the receiving end, I need to know the length of the image. What I intend to do is save the first 6 bytes of the buffer for the image size(since it seems to never exceed 6 numbers), and then from thereon have the rest of the buffer as the image. The problem I am having is I am not able to read only the first 6 bytes.
Server Code
int data = 0;
byte[] readBuffer = new byte[getSelectedClient().ReceiveBufferSize];
**data = stream.Read(readBuffer, 0, 5, readBuffer.Length);** <-- sort of thing im trying to do
string pictureSize = Encoding.ASCII.GetString(readBuffer, 0, data);
Console.WriteLine(pictureSize); //for debugging purposes
string x = new Random().Next().ToString();
FileStream f = new FileStream(x + ".bmp", FileMode.Create, FileAccess.Write);
while (new FileInfo(x + ".bmp").Length != Convert.ToInt32(pictureSize))
{
**data = stream.Read(readBuffer, 6, readBuffer.Length);** <-- then it would read from the 6th byte, which would be the start of the image
f.Write(readBuffer, 0, data);
}
f.Close();
Process.Start(x + ".bmp");
screenShotBTN.Enabled = true;
Client Code
MemoryStream ms = new MemoryStream();
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
writeToStream(combineBytes(Encoding.ASCII.GetBytes(ms.Length.ToString()), ms.ToArray()));
ms.Close();
public static byte[] combineBytes(byte[] b1, byte[] b2)
{
byte[] b3 = new byte[b1.Length + b2.Length];
Array.Copy(b1, 0, b3, 0, b1.Length);
Array.Copy(b2, 0, b3, b1.Length, b2.Length);
return b3;
}
What you are implementing is Length Prefixed messaging.
Things to remember are that when you are reading from a network stream, you will not necessarily receive all the bytes that you are looking for immediately (most times when you are testing locally everything will, but you need to program for the possibility you'll only receive 1 byte at the worst case).
Therefore you should go into a state (awaitinglength, awaitingpayload).
Then you commence reading byte(s) while in the awaiting length, then build up that data in a buffer. Once you have your length (6 bytes in your case), then you can switch state to awaitingpayload, then read this into your secondary buffer until you have the full payload and then you are good to go.
It might be worthwhile to have a read of Stephen Cleary's articles on message framing. I learn't a lot about networking through reading these.
http://blog.stephencleary.com/2009/04/sample-code-length-prefix-message.html
I solved the issue on my own.
Server
int data = 0;
byte[] readBuffer = new byte[getSelectedClient().ReceiveBufferSize];
data = stream.Read(readBuffer, 0, 6); //only read first 6
string pictureSize = Encoding.ASCII.GetString(readBuffer, 0, data);
Console.WriteLine(pictureSize); //for debugging purposes
string x = new Random().Next().ToString();
FileStream f = new FileStream(x + ".bmp", FileMode.Create, FileAccess.Write);
while (new FileInfo(x + ".bmp").Length != Convert.ToInt32(pictureSize))
{
data = stream.Read(readBuffer, 0, readBuffer.Length);
f.Write(readBuffer, 0, data);
}
f.Close();
Process.Start(x + ".bmp");
screenShotBTN.Enabled = true;
Client code
MemoryStream ms = new MemoryStream();
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Console.WriteLine(ms.Length.ToString());
writeToStream(combineBytes(Encoding.ASCII.GetBytes(ms.Length.ToString()), ms.ToArray()));
ms.Close();
I have a file transfer program. The program (Client) does following operations to send a bitmap via TCP socket: get screenshot -> grab Bitmap from memory -> convert to stream -> send
MemoryStream Fs = new MemoryStream();
//////////////11111
Bitmap bmp = TakeScreen();
///////////////2222
//Bitmap bmp = new Bitmap(#"C:\temp\001.bmp");
bmp.Save(Fs, ImageFormat.Bmp);
Byte[] buffer = Fs.ToArray();
Fs.Read(buffer, 0, buffer.Length);
TcpClient socket = new TcpClient("192.168.0.131", 1095);
NetworkStream nw = socket.GetStream();
nw.Write(buffer, 0, buffer.Length);
nw.Close();
Fs.Dispose();
socket.Close();
bmp.Dispose();
If I choose to transfer image directly from memory - no errors.
If I try to load Bitmap from file first - getting "Parameter is not valid" error on a server's side.
here is the server side:
NetworkStream Nw = new NetworkStream(handlerSocket.Client);
int thisRead = 0;
int Blocksize = 1024;
Byte[] dataByte = new Byte[Blocksize];
Bitmap screen = getScreen(Nw, dataByte, thisRead, Blocksize);
Nw.Close();
and
private Bitmap getScreen(NetworkStream Nw, Byte[] dataByte, int thisRead, int Blocksize)
{
Bitmap bitmap;
using (var strm = new MemoryStream())
{
while (true)
{
thisRead = Nw.Read(dataByte, 0, Blocksize);
strm.Write(dataByte, 0, thisRead);
if (thisRead == 0)
break;
}
bitmap = new Bitmap(strm); // Error here
}
Bitmap bm3 = new Bitmap(bitmap);
bitmap.Dispose();
return bm3;
}
What's causing this error? I guess it has something to do with the MemoryStream.
edit: simplified the question
You nee to seek to the beginning of the stream before you can create the Bitmap in getScreen.
private Bitmap getScreen(NetworkStream Nw, Byte[] dataByte, int thisRead, int Blocksize)
{
Bitmap bitmap;
using (var strm = new MemoryStream())
{
while (true)
{
thisRead = Nw.Read(dataByte, 0, Blocksize);
strm.Write(dataByte, 0, thisRead);
if (thisRead == 0)
break;
}
stream.Seek(0, SeekOrigin.Begin; // <-- Go Back to beginning of stream
bitmap = new Bitmap(strm); // Error here
}
Bitmap bm3 = new Bitmap(bitmap);
bitmap.Dispose();
return bm3;
}
EDIT
Detailed explanation: After writing the last byte to the stream, the stream's current position is a the end of the stream. Creating a Bitmap from the screen now tries to read the bitmap from the stream, which doesn't work, as there is no more data after the current position (= the end of the stream).
So what you need to do is tell the stream to set the current position back to the beginning of the stream. Then, the bitmap information can be read.
I want to preload a lot of images when my app starts up, sort of a once off thing.
I have an Image class that contains the Url of its image stored in the cloud as blob storage (this address is a https address BTW)
I want to download the image bytes from the cloud, store them on the object, then when it comes time to show the image, load the image from its bytes.
I have all the code for this, but I keep getting the exception:
No imaging component suitable to complete this operation was found.
Here is my code: EDIT UPDATED WITH A FIX
//Loaded on start-up
private static void LoadImageBytes(Image img)
{
var urlUri = new Uri(img.Url);
var request = (HttpWebRequest)WebRequest.CreateDefault(urlUri);
MemoryStream memStream = new MemoryStream();
using (var response = request.GetResponse())
{
var buffer = new byte[4096];
using (var stream = response.GetResponseStream())
{
int bytesRead = stream.Read(buffer, 0, buffer.Length);
while (bytesRead > 0)
{
memStream.Write(buffer, 0, bytesRead);
bytesRead = stream.Read(buffer, 0, buffer.Length);
}
img.ImageBytes = memStream.ToArray();
}
}
}
Then when I want to get the image on the screen I call this:
public BitmapImage ImageFromBuffer(Byte[] bytes)
{
MemoryStream stream = new MemoryStream(bytes);
stream.Seek(0, SeekOrigin.Begin);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
return image;
}
But in the EndInit() call I get the exception.
I have done some testing and if I load the file from my local filesystem, I get a different set of bytes to that of the image in the cloud. I assume its something to do with blob storage or https?
And yes I can browse to that image and its not corrupted.
EDIT, fixed now all good
Are you sure this line is correct?
while (stream.Read(buffer, 0, buffer.Length) > 0)
img.ImageBytes = buffer;
img.ImageBytes will hold the last read buffer.