Loading image from 32 bits bitmap
-
Friday, April 13, 2012 12:40 AM
I know theres another thread with the same name but this problem needs a better solution and I have searched it to death!
(code moved to a subsequent post)
thats my code but I occasionally get AccessViolationException on RotateFlip and of course that exception is unmanaged and cant be caught so I need a better solution and it needs to be fast this is for a multi client remote assistance type program and I get over 30fps.
- Edited by I installed vista on my bin Saturday, April 14, 2012 4:08 PM
All Replies
-
Friday, April 13, 2012 1:37 AMFree the pixels, then flip it.
-
Friday, April 13, 2012 2:29 AM
Its the same eather way.
It doesnt have to be saved to a file format, any way the bitmap object can be crammed into a byte[] to send over a socket is fine if theres a way to do that.
- Edited by I installed vista on my bin Friday, April 13, 2012 2:35 AM
-
Friday, April 13, 2012 5:31 AM
Its the same eather way.
It doesnt have to be saved to a file format, any way the bitmap object can be crammed into a byte[] to send over a socket is fine if theres a way to do that.
Post the complete code. The code defining the array and the code that fills the array. Copy and paste it, don't type it. -
Saturday, April 14, 2012 2:18 PMYou've posted code unrelated to the code you posted in your OP. Where do you define the array that you pin?
-
Saturday, April 14, 2012 2:40 PMI can't follow your code. You should have a managed array defined for your gchandle to pin. After it's pinned it should be filled by unmanaged code. I can't find the code that does this in the code you posted.
-
Saturday, April 14, 2012 3:00 PMYou were having problems with the code you originally posted. Since you have commented this code I guess your problem has been solved and this thread has been answered. I wasn't of any help.
-
Saturday, April 14, 2012 3:10 PMI'm still asking if theres a better way the new code is slower.
-
Saturday, April 14, 2012 3:14 PM
I'm still asking if theres a better way the new code is slower.
Slower than what? You never posted the original code that included the definition of the managed array that you pinned and how it was filled. -
Saturday, April 14, 2012 3:26 PM
here the frame is processed, compressed and sent over a socket to the next code block:
private void SendFrame(object bmp){ using (var frame = Resizebmp((Bitmap)bmp)){ //using (var ms = new MemoryStream()){ var fullframe = false; byte[] buf; if (_lastframe == null || _lastframe.Size != frame.Size){ //frame.Save(ms, ImageFormat.Bmp); buf = (byte[])TypeDescriptor.GetConverter(frame).ConvertTo(frame, typeof (byte[])); _lastframe = frame.Clone(new Rectangle(_p, frame.Size), PixelFormat.Format32bppArgb); fullframe = true; } else{ buf = (byte[])TypeDescriptor.GetConverter(frame).ConvertTo(BlankEqualPixels(_lastframe.Clone(new Rectangle(_p, frame.Size), PixelFormat.Format32bppArgb), frame, 0), typeof(byte[])); //BlankEqualPixels(_lastframe.Clone(new Rectangle(_p, frame.Size), PixelFormat.Format32bppArgb), frame, 0).Save(ms, ImageFormat.Bmp); _lastframe = frame.Clone(new Rectangle(_p, frame.Size), PixelFormat.Format32bppArgb); } try{ var compressedframe1 = QuickLZ.Compress(buf); var compressedframe = QuickLZ.Compress(compressedframe1); var size = BitConverter.GetBytes(compressedframe.Length); var scaled = new byte[1]; var fullframeb = new byte[1]; if (frame.Size != _screensize) scaled[0] = 1; if (fullframe) fullframeb[0] = 1; var proceed = new byte[1]; _vSock.Receive(proceed, 1, SocketFlags.None); if (proceed[0] == 1) _lastframe = null; _vSock.Send(BitConverter.GetBytes(frame.Width), 4, SocketFlags.None); _vSock.Send(BitConverter.GetBytes(frame.Height), 4, SocketFlags.None); _vSock.Send(scaled); _vSock.Send(fullframeb); _vSock.Send(size); _vSock.Send(compressedframe); _sw.Stop(); Invoke(new MethodInvoker(() => SetText(_sw.Elapsed.TotalMilliseconds))); _sw.Reset(); } catch (Exception e){ Invoke(new MethodInvoker(() => ConnectEvent(true))); if (!_stop) MessageBox.Show(Resources.Video_Stream_Error + e.Message); } _ok.Set(); } }
the frame is downloaded and sent to a threadpool thread:
private void ImageComplete(object state){ if (_cClient != null) try{ _sw0.Start(); //var gch = GCHandle.Alloc(QuickLZ.Decompress(QuickLZ.Decompress((byte[])state)), GCHandleType.Pinned); //var frame = new Bitmap(_res.Width, _res.Height, 4 * _res.Width, PixelFormat.Format32bppArgb, (IntPtr)((int)(gch.AddrOfPinnedObject()) + 54)); //gch.Free(); var frame = (Bitmap)TypeDescriptor.GetConverter(typeof (Bitmap)).ConvertFrom(QuickLZ.Decompress(QuickLZ.Decompress((byte[])state))); if (frame != null){ //frame.RotateFlip(RotateFlipType.RotateNoneFlipY); frame.SetResolution(96, 96); if (pictureBox1.Image == null || _pbg == null){ while (pictureBox1.Image == null) Invoke(new MethodInvoker(() => pictureBox1.Image = frame.Clone(new Rectangle(0, 0, frame.Width, frame.Height), PixelFormat.Format32bppArgb))); _pbg = Graphics.FromImage(pictureBox1.Image); } else{ _pbg.DrawImage(frame, 0, 0); frame.Dispose(); } if ((int)_pbg.VisibleClipBounds.Size.Width != _res.Width || (int)_pbg.VisibleClipBounds.Size.Height != _res.Height) _switched = true; Invoke(new MethodInvoker(pictureBox1.Refresh)); } } catch (NullReferenceException){} catch(InvalidOperationException){} finally{ _sw0.Stop(); if (_cClient != null) Invoke( new MethodInvoker(() => toolStripStatusLabel1.Text = Resources.Client_Name__ + _activeclient + Resources._Client_IP__ + _cClient.RemoteEndPoint + Resources.__Frame_Download_Time__ + _sw1Time + Resources._ms__Draw_time__ + _sw0.Elapsed.TotalMilliseconds + Resources._ms__Total_Time__ + (_sw0.Elapsed.TotalMilliseconds + _sw1Time) + Resources._ms)); _sw0.Reset(); _ok.Set(); } }
ORIGINAL CODE is commented with //
- Edited by I installed vista on my bin Saturday, April 14, 2012 3:47 PM
-
Saturday, April 14, 2012 3:55 PMAll I wanted is the fastest way of cramming a bitmap into a byte[] and you asked me to post all my code if you cant understand it then why ask for it?
-
Saturday, April 14, 2012 4:02 PM
All I wanted is the fastest way of cramming a bitmap into a byte[] and you asked me to post all my code if you cant understand it then why ask for it?
Please read your original post. "get AccessViolationException on RotateFlip and of course thats unmanaged" I asked for code that completed the code in the original post. -
Saturday, April 14, 2012 4:06 PM
private static byte[] BmpToBytes(Bitmap bmp){ BitmapData bData = null; byte[] bmpBytes = null; try{ bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); var byteCount = bData.Stride*bmp.Height; bmpBytes = new byte[byteCount]; Marshal.Copy(bData.Scan0, bmpBytes, 0, byteCount); } catch{} finally{ try{ if (bmp != null) bmp.UnlockBits(bData); } catch{} } return bmpBytes; }
private Bitmap BytesToBmp(byte[] bmpBytes, Size imageSize){ if (imageSize.Width == 0 || imageSize.Height == 0) return null; BitmapData bData = null; Bitmap bmp = null; try{ bmp = new Bitmap(imageSize.Width, imageSize.Height); bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Marshal.Copy(bmpBytes, 0, bData.Scan0, bmpBytes.Length); } catch{} finally{ try{ if (bmp != null) bmp.UnlockBits(bData); } catch{} } return bmp; }
- Marked As Answer by I installed vista on my bin Sunday, April 15, 2012 1:28 AM
- Edited by I installed vista on my bin Sunday, April 15, 2012 1:29 AM

