pictureBox.InvokeRequired not showing img - c#

I'm receiving from socket communication an image as byte[] and then I try to show it in a pictureBox. When I run the code it shows a message error only saying: "NullReferenceException"
The catch handling the exception is ex1 and I checked and the pic isn't null so I can't figure it out why this exception is happening.
This is my code:
try
{
if (pictureBox1.InvokeRequired)
{
try
{
pic = imageEmp;
addControlHandler c = new addControlHandler(addPic);
this.Invoke(c);
}
catch (Exception exc) { MessageBox.Show(exc.Message); }
}
else
{
pictureBox1.Image = ByteToImage(imageEmp);
}
}
catch (Exception ex1)
{
MessageBox.Show(ex1.Message);
}
public void addPic() //when invokeRequired == true
{
pictureBox1.Image = ByteToImage(pic);
}
Here is the code to convert byte[] to Image:
public Image ByteToImage(byte[] imageBytes) //convert byte[] to image
{
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = new Bitmap(ms);
return image;
}
UPDATE 1: Regarding Hans answer, I make he following changes:
Change my ByteToImage to Hans's answer and to check where the error is I added this lines in the place where this.Invoke(c) was:
if (c != null)
{
try
{
this.Invoke(c);
}
catch (Exception e_c)
{
MessageBox.Show(e_c.Message, "exc from e_c");
}
}
This give me an exception : NullReferenceException
Thanks for any help!
UPDATE 2: Now it's working, I send JPEG images instead of JPG and it show it now. Don't know why this happens but now it's working ok.

Here is an example that you can try I have tested this just now using my own method this works so replace your code with the lines of code in my btnStudentPic_Click let me know if this works for you..
For Compact Framework try this instead
public static byte[] ReadAllBytes(string path)
{
byte[] buffer;
using (FileStream fs = new FileStream(path, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
int offset = 0;
int count = (int)fs.Length;
buffer = new byte[count];
while (count > 0)
{
int bytesRead = fs.Read(buffer, offset, count);
offset += bytesRead;
count -= bytesRead;
}
}
return buffer;
}
//Windows example below don't use for CF
private void btnStudentPic_Click(object sender, EventArgs e)
{
Image picture = (Image)BrowseForPicture();
this.picStudent.Image = picture;
this.picStudent.SizeMode = PictureBoxSizeMode.StretchImage;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private Bitmap BrowseForPicture()
{
// Bitmap picture = null;
try
{
if (this.fdlgStudentPic.ShowDialog() == DialogResult.OK)
{
byte[] imageBytes = File.ReadAllBytes(this.fdlgStudentPic.FileName);
StudentPic = new Bitmap( this.fdlgStudentPic.FileName);
StuInfo.StudentPic = imageBytes;
}
else
{
StudentPic = Properties.Resources.NoPhotoAvailable;
}
}
catch (Exception)
{
MessageBox.Show("That was not a picture.", "Browse for picture");
StudentPic = this.BrowseForPicture();
}
return StudentPic;
}

Related

C# System.AccessviolationException and using GC.Collect

Here is the code sample:
ConcurrentCircularBuffer<byte[]> _cList = new ConcurrentCircularBuffer<byte[]>(10);
private Bitmap getBitmap()
{
byte[] bmpdata = _cList.Dequeue();
if (bmpdata != null && bmpdata.Length > 0)
{
GCHandle handle = GCHandle.Alloc(bmpdata, GCHandleType.Pinned);
try
{
Bitmap bmp = new Bitmap(1920, 1080, stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, handle.AddrOfPinnedObject());
return bmp;
}
catch (Exception ex)
{}
finally
{
handle.Free();
}
}
return null;
}
public string GetBase64Image()
{
Bitmap bmp = getBitmap();
if (bmp != null)
{
using (var ms = new MemoryStream())
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); // Here I got the System.AccessViolationException
byte[] buffer = ms.ToArray();
return Convert.ToBase64String(buffer);
}
}
}
In this case after few attempts of converting image to base64string I getting the AccessViolationException.
When I'm adding 'GC.Collect()' to my 'Finally' block, the exception doesn't appear.
What am I doing wrong?
Is there a way to avoid the using of GC.Collect()?
Source image - is the live-stream from IP-camera, that writes the byte array to implementation of ConcurrentQueue that stores last 10 images.
upd: exception thrown in the multi-thread with low latency between the executions.
upd2:
ConcurrentCircularBuffer<byte[]> _cList = new ConcurrentCircularBuffer<byte[]>(10);
public string GetBase64Image()
{
byte[] bmpdata = _cList.Dequeue();
if (bmpdata != null && bmpdata.Length > 0)
{
GCHandle handle = GCHandle.Alloc(bmpdata, GCHandleType.Pinned);
try
{
using (Bitmap bmp = new Bitmap(1920, 1080, stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, handle.AddrOfPinnedObject()))
{
using (var ms = new MemoryStream())
{
bmp.Save(ms, ImageFormat.Png);
return Convert.ToBase64String(ms.ToArray());
}
}
}
finally
{
handle.Free();
GC.Collect();
}
}
}
At this time is the best solution. Without GC.Collect() - accessviolation when saving bitmap to MemoryStream

c# download the extracted image

I have created a Form Application to Extracted Image , I have searched many posts , till Now I am able to download into the MemoryStream (Byte Array ).
I am not able to save that byte array into file system and check the image size ....
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public List<string> FetchImages(string Url)
{
List<string> imageList = new List<string>();
if (!Url.StartsWith("http://") && !Url.StartsWith("https://"))
Url = "http://" + Url;
string responseUrl = string.Empty;
string htmlData = ASCIIEncoding.ASCII.GetString(DownloadData(Url, out responseUrl));
if (responseUrl != string.Empty)
Url = responseUrl;
if (htmlData != string.Empty)
{
string imageHtmlCode = "<img";
string imageSrcCode = #"src=""";
int index = htmlData.IndexOf(imageHtmlCode);
while (index != -1)
{
htmlData = htmlData.Substring(index);
int brackedEnd = htmlData.IndexOf('>'); //make sure data will be inside img tag
int start = htmlData.IndexOf(imageSrcCode) + imageSrcCode.Length;
int end = htmlData.IndexOf('"', start + 1);
if (end > start && start < brackedEnd)
{
string loc = htmlData.Substring(start, end - start);
imageList.Add(loc);
}
if (imageHtmlCode.Length < htmlData.Length)
index = htmlData.IndexOf(imageHtmlCode, imageHtmlCode.Length);
else
index = -1;
}
for (int i = 0; i < imageList.Count; i++)
{
string img = imageList[i];
string baseUrl = GetBaseURL(Url);
if ((!img.StartsWith("http://") && !img.StartsWith("https://"))
&& baseUrl != string.Empty)
img = baseUrl + "/" + img.TrimStart('/');
imageList[i] = img;
}
}
return imageList;
}
private byte[] DownloadData(string Url)
{
string empty = string.Empty;
return DownloadData(Url, out empty);
}
private byte[] DownloadData(string Url, out string responseUrl)
{
byte[] downloadedData = new byte[0];
try
{
WebRequest req = WebRequest.Create(Url);
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
responseUrl = response.ResponseUri.ToString();
byte[] buffer = new byte[1024];
MemoryStream memStream = new MemoryStream();
while (true)
{
int bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
break;
}
else
{
memStream.Write(buffer, 0, bytesRead);
}
}
downloadedData = memStream.ToArray();
stream.Close();
memStream.Close();
}
catch (Exception)
{
responseUrl = string.Empty;
return new byte[0];
}
return downloadedData;
}
private Image ImageFromURL(string Url)
{
byte[] imageData = DownloadData(Url);
Image img = null;
try
{
MemoryStream stream = new MemoryStream(imageData);
img = Image.FromStream(stream);
stream.Close();
}
catch (Exception)
{
}
return img;
}
private string GetBaseURL(string Url)
{
int inx = Url.IndexOf("://") + "://".Length;
int end = Url.IndexOf('/', inx);
string baseUrl = string.Empty;
if (end != -1)
return Url.Substring(0, end);
else
return string.Empty;
}
private void btnGetImages_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
listImages.Items.Clear();
foreach (string image in FetchImages(txtURL.Text))
{
listImages.Items.Add(image);
}
this.Cursor = Cursors.Default;
}
private void btnView_Click(object sender, EventArgs e)
{
if (listImages.SelectedIndex != -1)
picImage.Image = ImageFromURL(listImages.SelectedItem.ToString());
}
private void btnSave_Click(object sender, EventArgs e)
{
}
private void btnDownload_Click(object sender, EventArgs e)
{
DownloadData(txtURL.Text);
}
.. I am trying to save the memroy stream into hard drive but still not getting exact code ,
i think this code needs some additional changes
MemoryStream memStream = new MemoryStream();
while (true)
{
//Try to read the data
int bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
break;
}
else
{
//Write the downloaded data
memStream.Write(buffer, 0, bytesRead);
}
}
Any Help will be appreciated
If you want to save the Byte[] array to a file,
Syntax : File.WriteAllBytes(string path, byte[] bytes)
Eg : File.WriteAllBytes("Foo.txt", arrBytes); // Requires System.IO
refer this link for more info http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx
If you want to convert the Byte back to image and save to the drive.
Eg:
byte[] bitmap = YourImage();
using(Image image = Image.FromStream(new MemoryStream(bitmap)))
{
image.Save("image.jpg", ImageFormat.Jpeg);
}

C# with ZXing.Net: Decoding the QR code

I'm new to C# and got problem with QR code decoding using ZXing.Net. The application launches with no errors, but I get nothing in the result string. I think the problem could be in RGBLuminanceSource().
private static byte[] ToByteArray(Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
private void button1_Click(object sender, EventArgs e)
{
*** SOME OTHER CODE HERE ***
Bitmap BitmapImage = new Bitmap(#"D:\1.png");
QRCodeReader reader = new QRCodeReader();
LuminanceSource source = new RGBLuminanceSource(ToByteArray(BitmapImage), BitmapImage.Width, BitmapImage.Height);
var binarizer = new HybridBinarizer(source);
var binBitmap = new BinaryBitmap(binarizer);
string result = reader.decode(binBitmap).Text;
*** SOME OTHER CODE HERE ***
}
Just call function. Also, replace ... with your handling
public Result decode(Uri uri)
{
Bitmap image;
try
{
image = (Bitmap) Bitmap.FromFile(uri.LocalPath);
}
catch (Exception)
{
throw new FileNotFoundException("Resource not found: " + uri);
}
using (image)
{
LuminanceSource source;
source = new BitmapLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap);
if (result != null)
{
... code found
}
else
{
... no code found
}
return result;
}
}

Show only small part of picture

I have write a program that consists server and client. The server makes a screenshot desktop, and the client receives the screenshot and shows on the form. There is a problem in the data transfer, I see only a small strip. I checked before sending the image on the server is ok. However, the client receives the wrong image. What is wrong?
XAML:
<Window x:Class="TestTCP.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestTCP" Height="350" Width="525">
<Grid>
<Image x:Name="img1" Margin="0"/>
</Grid>
</Window>
Code C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Task rec = new Task(WaitClientQuery);
rec.Start();
Task tk = new Task(Send);
tk.Start();
}
private void Send()//server part. take screen and send image
{
Bitmap temp = CaptureScreen(true);
MemoryStream ms3 = new MemoryStream();
BitmapImage image = new BitmapImage();
byte[] img_byte;
try
{
((System.Drawing.Bitmap)temp).Save(ms3, System.Drawing.Imaging.ImageFormat.Bmp);
image.BeginInit();
ms3.Seek(0, SeekOrigin.Begin);
image.StreamSource = ms3;
image.EndInit();
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
img_byte = ms.ToArray();
}
try
{
TcpClient client = new TcpClient("192.168.1.64", 4444);
NetworkStream netstream = client.GetStream();
netstream.Write(img_byte, 0, img_byte.Length);
netstream.Close();
client.Close();
}
catch (Exception)
{
}
}
catch (Exception ee)
{
}
}
[StructLayout(LayoutKind.Sequential)]
struct CURSORINFO
{
public Int32 cbSize;
public Int32 flags;
public IntPtr hCursor;
public POINTAPI ptScreenPos;
}
[StructLayout(LayoutKind.Sequential)]
struct POINTAPI
{
public int x;
public int y;
}
[DllImport("user32.dll")]
static extern bool GetCursorInfo(out CURSORINFO pci);
[DllImport("user32.dll")]
static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon);
const Int32 CURSOR_SHOWING = 0x00000001;
public static Bitmap CaptureScreen(bool CaptureMouse)
{
Bitmap result = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
try
{
using (Graphics g = Graphics.FromImage(result))
{
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
if (CaptureMouse)
{
CURSORINFO pci;
pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CURSORINFO));
if (GetCursorInfo(out pci))
{
if (pci.flags == CURSOR_SHOWING)
{
DrawIcon(g.GetHdc(), pci.ptScreenPos.x, pci.ptScreenPos.y, pci.hCursor);
g.ReleaseHdc();
}
}
}
}
}
catch
{
result = null;
}
return result;
}
void WaitClientQuery()//receive data(client)
{
try
{
IPAddress ip ;
IPAddress.TryParse("192.168.1.64", out ip);
TcpListener listener = new TcpListener(ip, 4444);
listener.Start();
while (true)
{
TcpClient client = listener.AcceptTcpClient();
Thread thread = new Thread(new ParameterizedThreadStart(ReadMessage));
thread.IsBackground = true;
thread.Start(client);
}
}
catch (Exception ex)
{
}
}
void ReadMessage(object obj)
{
try
{
TcpClient client = (TcpClient)obj;
NetworkStream netstream = client.GetStream();
byte[] arr = new byte[client.ReceiveBufferSize ];
int len = netstream.Read(arr, 0, client.ReceiveBufferSize);
if (len > 0)
{
try
{
Action act = delegate
{
MemoryStream strmImg = new MemoryStream(arr);
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.StreamSource = strmImg;
myBitmapImage.EndInit();
img1.Source = myBitmapImage;
};
this.Dispatcher.BeginInvoke(act);
}
catch (Exception ex)
{
string message = ex.Message;
}
}
netstream.Close();
client.Close();
}
catch (Exception ex)
{
}
}
}
P.S. Result, screen program after compile:
click to view
The main root cause the client.ReceiveBufferSize using default value are 8192 byte so the client will not receive enough image which has been transferred from server. In my code, I would like to see at client receive correctly the size of image so I use BinaryWriter to transfer length of image. From client side I have used BinaryReader to read exactly number of bytes and size of image to save bandwidth. Hope this help
At the Send method:
TcpClient client = new TcpClient("192.168.1.64", 4444);
using (NetworkStream netstream = client.GetStream())
{
using (BinaryWriter bw = new BinaryWriter(netstream))
{
bw.Write(img_byte.Length);
bw.Write(img_byte, 0, img_byte.Length);
}
client.Close();
}
At the ReadMessage(object obj)
private void ReadMessage(object obj)
{
try
{
byte[] arr = null;
TcpClient client = (TcpClient) obj;
using (NetworkStream netstream = client.GetStream())
{
using (BinaryReader br = new BinaryReader(netstream))
{
var arrLen = new byte[4];
br.Read(arrLen, 0, 4);
int len = BitConverter.ToInt32(arrLen, 0);
if (len > 0)
{
arr = new byte[len];
int read = 0;
while (read != len)
{
read += br.Read(arr, read, arr.Length - read);
}
}
}
}
if (arr != null && arr.Length > 0)
{
try
{
Action act = delegate
{
MemoryStream strmImg = new MemoryStream(arr);
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.StreamSource = strmImg;
myBitmapImage.EndInit();
img1.Source = myBitmapImage;
};
this.Dispatcher.BeginInvoke(act);
}
catch (Exception ex)
{
string message = ex.Message;
}
}
client.Close();
}
catch (Exception ex)
{
}
}
I expect client.ReceiveBufferSize on client side has wrong information
I would recomend to read data in loop.
if(netstream.CanRead)
{
byte[] myReadBuffer = new byte[1024];
int numberOfBytesRead = 0;
do
{
numberOfBytesRead = netstream.Read(myReadBuffer, 0, myReadBuffer.Length);
}
while(netstream.DataAvailable);
}
and save data into the List or memorystream
Or may be easier way, before your server send all data, send the size
netstream.Write(img_byte.Length); //does not work with NetworkStream
netstream.Write(img_byte, 0, img_byte.Length);
and client can read it first to allocate appropriate buffer.
To send and receive particular data type I would recomend
build BinaryWriter from your NetworkStream
using (BinaryWriter writer = new BinaryWriter(netstream))

FileStream and ObjectDisposedException in .NET CF

public byte[] GetFile(string filename)
{
FileStream aStream = File.Open(filename, FileMode.Open, FileAccess.Read);
BinaryReader binReader = new BinaryReader(aStream);
binReader.BaseStream.Position = 0;
byte[] binFile = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length));
binReader.Close();
return binFile;
}
I run this method for a number of filepaths, problem is whenever a file cannot be accessed with File.Open (because it is used by another process) I get:
'aStream.Position' threw an exception of type 'System.ObjectDisposedException'
on the following line:
binReader.BaseStream.Position = 0;
And seldom I get
{System.IO.IOException: The process can not access the file '\folder\file.txt' because it is being used by another process.}
This is the exception I want. So why is the object disposed most of the times?
Note: I first had the FileStream line in a using statement but removed it because I thought that might have disposed the object. But the problem remains.
Edit: Using Compact Framework, which doesn't have ReadAllBytes.
Maybe that part of the time your FileStream is throwing the IOException whenever your file is in use, and at other times, perhaps, you are getting the ObjectDisposedException because your array is not initialized.
Obviously, I can not test this theory.
See if you can copy-n-paste this one with good results:
public byte[] GetFile(string filename)
{
byte[] binFile = null;
try
{
using (var aStream = File.Open(filename, FileMode.Open, FileAccess.Read))
{
BinaryReader binReader = new BinaryReader(aStream);
binFile = new byte[binReader.BaseStream.Length];
binReader.BaseStream.Position = 0; // <= this step should not be necessary
binFile = binReader.ReadBytes(binReader.BaseStream.Length);
binReader.Close();
}
} catch (IOException err) {
// file is being used by another process.
} catch (ObjectDisposedException err) {
// I am guessing you would never see this because your binFile is not disposed
}
return binFile;
}
Be sure to check for null return variables!
EDIT:
I wrote (what I think is) a simpler version. I tested it, and it seems to work OK. I also prefer Read() overload to ReadBytes(), because I know how much data was pulled in.
First, is the test function that calls the method for every image in my Pictures folder:
public void Test() {
DirectoryInfo dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
foreach (var subDir in dir.GetDirectories()) {
if (-1 < subDir.Name.ToLower().IndexOf("pictures")) {
foreach (var file in subDir.GetFiles()) {
byte[] data = GetFile(file.FullName);
if (data != null) {
Console.WriteLine(data.Length);
}
}
}
}
}
public byte[] GetFile(string filename) {
byte[] result = null;
try {
if (File.Exists(filename)) {
int len = 0;
FileInfo file = new FileInfo(filename);
byte[] data = new byte[file.Length];
using (BinaryReader br = new BinaryReader(file.Open(FileMode.Open, FileAccess.Read))) {
len = br.Read(data, 0, data.Length);
br.Close();
}
if (0 < len) {
if (len == data.Length) {
return data;
} else {
// this section of code was never triggered in my tests;
// however, it is good to keep it as a backup.
byte[] dat2 = new byte[len];
Array.Copy(data, dat2, len);
return dat2;
}
}
}
} catch (IOException err) {
// file is being used by another process.
} catch (ObjectDisposedException err) {
// I am guessing you would never see this because your binFile is not disposed
}
return result;
}
I don't see any reason why these would not work - unless you are having an int overflow.
Just use this:
byte[] contents = File.ReadAllBytes(filename);
Why don't you simply use
public byte[] GetFile(string filename)
{
try { return File.ReadAllBytes(filename); }
catch { return null; }
}
Just for fun, you could even define an extension method
public static class Extensions
{
public static byte[] GetFile(this string filename)
{
try { return File.ReadAllBytes(filename); }
catch { return null; }
}
}
so you could do byte[] myfile = filename.GetFile();.
Remember you must check that return is not null before proceed:
if (myfile != null)
{
// Do what you need
}

Categories