using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using ZyDebugPlatform.UI.Disorder.ViewModels;
using Image = System.Windows.Controls.Image;
namespace ZyDebugPlatform.UI.Disorder.DHCamera
{
public class BitMaps:IDisposable
{
static private AlgorithmC.RealCallback realCallBack = null;
static private AlgorithmC.CaptureCallback captureCallback = null;
Action<bool> action1 = null;
Action<BitmapImage, bool> updatecallback;
CWin32Bitmaps.BITMAPINFO m_objBitmapInfo = new CWin32Bitmaps.BITMAPINFO();
IntPtr m_pHDC = IntPtr.Zero;
IntPtr m_pBitmapInfo = IntPtr.Zero;
PictureBox pc;
Image image;
System.Drawing.Graphics m_objGC = null;
int mwidht = 1000;
int mheight = 1000;
public void RealCallbackFun(IntPtr buffer, int width, int height)
{
action1?.Invoke(true);
//檢查圖像是否改變並更新Buffer
__UpdateBufferSize(width, height);
byte[] byMonoBufferTmp = new byte[width * height * 3];
Marshal.Copy(buffer, byMonoBufferTmp, 0, width * 3 * height);
if (null != pc)
{
CWin32Bitmaps.SetStretchBltMode(m_pHDC, 3);
CWin32Bitmaps.StretchDIBits(
m_pHDC,
0,
0,
(int)pc.Width,
(int)pc.Height,
0,
0,
width,
height,
byMonoBufferTmp,
m_pBitmapInfo,
0,
0x00CC0020);
}
}
public void RealCallbackFun(IntPtr buffer, int width, int height,bool isfocuse)
{
//檢查圖像是否改變並更新Buffer
__UpdateBufferSize(width, height);
byte[] byMonoBufferTmp = new byte[width * height * 3];
Marshal.Copy(buffer, byMonoBufferTmp, 0, width * 3 * height);
if (null != pc)
{
CWin32Bitmaps.SetStretchBltMode(m_pHDC, 3);
CWin32Bitmaps.StretchDIBits(
m_pHDC,
0,
0,
(int)pc.Width,
(int)pc.Height,
0,
0,
width,
height,
byMonoBufferTmp,
m_pBitmapInfo,
0,
0x00CC0020);
}
}
public void ImageCallback(IntPtr buffer, int width, int height)
{
var CameraData = new Bitmap(width, height, width * 3, PixelFormat.Format24bppRgb, buffer);
BitmapImage objdataImage = BitmapToBitmapImage(CameraData);
//image.Dispatcher.BeginInvoke(new Action(()=> { image.Source = objdataImage; }));
updatecallback?.Invoke(objdataImage,false);
}
/// <summary>
/// 檢查圖像是否改變並更新Buffer
/// </summary>
/// <param name="objIBaseData">圖像數據對象</param>
private void __UpdateBufferSize(int nwidth, int nheight)
{
if (nwidth != mwidht || nheight != mheight)
{
//更新BitmapInfo
m_objBitmapInfo.bmiHeader.biWidth = nwidth;
m_objBitmapInfo.bmiHeader.biHeight = nheight;
Marshal.StructureToPtr(m_objBitmapInfo, m_pBitmapInfo, false);
}
}
public BitMaps(PictureBox _pc)
{
pc = _pc;
bitmap();
m_objGC = pc.CreateGraphics();
m_pHDC = m_objGC.GetHdc();
realCallBack = RealCallbackFun;
//captureCallback = RealCallbackFun;
AlgorithmC.StartRealPlay(realCallBack);
//AlgorithmC.OpenStream(captureCallback);
}
public BitMaps(Action<BitmapImage,bool> _updatecallback) {
realCallBack = ImageCallback;
updatecallback = _updatecallback;
AlgorithmC.StartRealPlay(realCallBack);
}
public BitMaps(PictureBox _pc, Action<bool> _action)
{
pc = _pc;
bitmap();
m_objGC = pc.CreateGraphics();
m_pHDC = m_objGC.GetHdc();
realCallBack = RealCallbackFun;
action1 = _action;
AlgorithmC.StartRealPlay(realCallBack);
}
public BitMaps(Image _image, Action<bool> _action)
{
image = _image;
action1 = _action;
realCallBack = ImageCallback;
AlgorithmC.StartRealPlay(realCallBack);
}
void bitmap()
{
m_objBitmapInfo.bmiHeader.biSize = (uint)Marshal.SizeOf(typeof(CWin32Bitmaps.BITMAPINFOHEADER));
m_objBitmapInfo.bmiHeader.biWidth = 648;
m_objBitmapInfo.bmiHeader.biHeight = 1800;
m_objBitmapInfo.bmiHeader.biPlanes = 1;
m_objBitmapInfo.bmiHeader.biBitCount = 24;
m_objBitmapInfo.bmiHeader.biCompression = 0;
m_objBitmapInfo.bmiHeader.biSizeImage = 0;
m_objBitmapInfo.bmiHeader.biXPelsPerMeter = 0;
m_objBitmapInfo.bmiHeader.biYPelsPerMeter = 0;
m_objBitmapInfo.bmiHeader.biClrUsed = 0;
m_objBitmapInfo.bmiHeader.biClrImportant = 0;
m_pBitmapInfo = Marshal.AllocHGlobal(2048);
Marshal.StructureToPtr(m_objBitmapInfo, m_pBitmapInfo, false);
}
private BitmapImage BitmapToBitmapImage(Bitmap bitmap)
{
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Png);
stream.Position = 0;
BitmapImage result = new BitmapImage();
result.BeginInit();
result.CacheOption = BitmapCacheOption.OnLoad;
result.StreamSource = stream;
result.EndInit();
result.Freeze();
return result;
}
}
public void Dispose()
{
pc = null;
m_pHDC = IntPtr.Zero;
m_pBitmapInfo = IntPtr.Zero;
}
}
}