C# 来自bytearray故障的图像
我有一个函数叫做:display和saveImagefromByteArray 通过它的名字,你可能明白我在做什么。在bytearray中,值是像素数据。所以像这个255220130,0等等 字节的大小是图像的宽度和高度乘以4。 因为它可以大踏步地工作C# 来自bytearray故障的图像,c#,arrays,image,bitmap,C#,Arrays,Image,Bitmap,我有一个函数叫做:display和saveImagefromByteArray 通过它的名字,你可能明白我在做什么。在bytearray中,值是像素数据。所以像这个255220130,0等等 字节的大小是图像的宽度和高度乘以4。 因为它可以大踏步地工作 public void DisplayAndSaveImageFromByteArray(byte[] byteArray) { try{ byte[] data = new byte[width * height * 4
public void DisplayAndSaveImageFromByteArray(byte[] byteArray)
{
try{
byte[] data = new byte[width * height * 4];
int o = 0;
for (int io = 0; io < width * height; io++){
byte value = byteArray[io];
data[o++] = value;
data[o++] = value;
data[o++] = value;
data[o++] = 0;
}
unsafe
{
fixed (byte* ptr = data)
{
using (image = new Bitmap((int)width, (int)height, (int)width * 4,System.Drawing.Imaging.PixelFormat.Format32bppRgb, new IntPtr(ptr)))
{
image.Save(@"c:\\testmap\" + nextpicture + ".jpg", ImageFormat.Jpeg);
if (nextpicture >= 10)
{
pbCameraPreview.BeginInvoke(new InvokeDelegate(InvokeMethod));
}
nextpicture++;
}
}
}
}
catch (Exception ex){
MessageBox.Show(ex.ToString());
}
}
public void display和saveImagefromByteArray(字节[]byteArray)
{
试一试{
字节[]数据=新字节[宽度*高度*4];
INTO=0;
用于(int io=0;io=10)
{
pbCameraPreview.BeginInvoke(新的InvokeDelegate(InvokeMethod));
}
nextpicture++;
}
}
}
}
捕获(例外情况除外){
Show(例如ToString());
}
}
当我运行此代码时,它将工作,但只有当所有值都相同时,例如:白色(255255)或黑色(0,0,0)。
它能够在RGB(A)值上下偏离约5,直到停止工作。
但是一旦图像的颜色改变,它就会停止工作,而不会给我任何例外
唯一的错误/异常是,如果我让它打开一分钟,VS会发现正在执行的代码没有做任何事情,并会给我一个警告。>上下文切换死锁
我做错了什么让它崩溃了?
解决办法是什么?
由于某些原因,它不允许我使用和命名空间名称。。。
(更新)完整代码:
public Form1()
{
InitializeComponent();
pbCameraPreview.Image = defImg;
}
#region Global
int ii;
object __p1;
EventArgs __p2;
string path;
Image defImg = Image.FromFile(@"c:\\testimg\def.jpg");
UInt32 width;
UInt32 height;
int nextpicture = 0;
FGNodeInfoContainer InfoContainer = new FGNodeInfoContainer();
FGNodeInfo NodeInfo = new FGNodeInfo();
FireWrap_CtrlCenter CtrlCenter;
enFireWrapResult Result;
UInt32 XSize = new UInt32();
UInt32 YSize = new UInt32();
UInt32 NodeCnt;
public delegate void InvokeDelegate();
Bitmap image;
CameraCode Cam = new CameraCode();
FGFrame Frame = new FGFrame();
FGUIntHL Guid = new FGUIntHL();
#endregion
public void CheckDirectory()
{
path = @"c:\\testmap\" + ii + "\\";
if (Directory.Exists(@"c:\\testmap\") == false)
{
Directory.CreateDirectory(@"c:\\testmap\");
}
if (Directory.Exists(path))
{
if (File.Exists(path + "0.Jpeg"))
{
ii++;
CheckDirectory();
}
}
else
{
Directory.CreateDirectory(path);
}
}
//Haal de images op
/// <param name="__p1"></param>
/// <param name="__p2"></param>
public void btStart_Click(object sender, EventArgs e)
{
Debug.WriteLine("btStart_Click is clicked");
// Init module
CtrlCenter = FireWrap_CtrlCenter.GetInstance();
Result = CtrlCenter.FGInitModule();
// Register frame start event
CtrlCenter.OnFrameReady += new FireWrap_CtrlCenter.FireWrapEvent(OnFrameReady);
// Get list of connected nodes
if (Result == enFireWrapResult.E_NOERROR)
{
Result = InfoContainer.FGGetNodeList();
NodeCnt = InfoContainer.Size();
// Print Nodecnt
Console.WriteLine(NodeCnt.ToString() + " camera found");
// Connect with first node
InfoContainer.GetAt(NodeInfo, 0);
Result = Cam.Connect(NodeInfo.Guid);
if (Result == enFireWrapResult.E_NOERROR)
{
Cam.m_Guid = NodeInfo.Guid;
}
// Set Format7 Mode0 Y8
if (Result == enFireWrapResult.E_NOERROR)
{
Result = Cam.SetParameter(enFGParameter.E_IMAGEFORMAT,
(uint)(((uint)enFGResolution.E_RES_SCALABLE << 16) |
((uint)enColorMode.E_CCOLORMODE_Y8 << 8) |
0));
}
if (Result != enFireWrapResult.E_NOERROR)
{
Result = Cam.SetParameter(enFGParameter.E_IMAGEFORMAT,
(uint)(((uint)enFGResolution.E_RES_SCALABLE << 16) |
((uint)enColorMode.E_CCOLORMODE_Y8 << 8) |
1));
}
// Start DMA logic
if (Result == enFireWrapResult.E_NOERROR)
Result = Cam.OpenCapture();
// Print device settings
Result = Cam.GetParameter(enFGParameter.E_XSIZE, ref XSize);
Result = Cam.GetParameter(enFGParameter.E_YSIZE, ref YSize);
Debug.WriteLine(Cam.DeviceAll + " [" + Cam.m_Guid.Low.ToString() + "] " + XSize + "x" + YSize);
width = XSize;
height = YSize;
// Start camera
if (Result == enFireWrapResult.E_NOERROR)
{
Result = Cam.StartDevice();
}
}
}
public void btStop_Click(object sender, EventArgs e)
{
// Stop the device
Cam.StopDevice();
// Close capture
Cam.CloseCapture();
// Disconnect before ExitModule
Cam.Disconnect();
// Exit module
CtrlCenter.FGExitModule();
}
/// <param name="__p1"></param>
/// <param name="__p2"></param>
public void OnFrameReady(object __p1, EventArgs __p2)
{
Debug.WriteLine("OnFrameReady is called");
FGEventArgs args = (FGEventArgs)__p2;
Guid.High = args.High;
Guid.Low = args.Low;
if (Guid.Low == Cam.m_Guid.Low)
{
Result = Cam.GetFrame(Frame, 0);
// Process frame, skip FrameStart notification
if (Result == enFireWrapResult.E_NOERROR & Frame.Length > 0)
{
byte[] data = new byte[Frame.Length];
// Access to frame data
if (Frame.CloneData(data))
{
DisplayAndSaveImageFromByteArray(data);
// Here you can start your image processsing logic on data
string debug = String.Format("[{6}] Frame #{0} length:{1}byte [ {2} {3} {4} {5} ... ]",
Frame.Id, Frame.Length, data[0], data[1], data[2], data[3], Cam.m_Guid.Low);
Debug.WriteLine(debug);
}
// Return frame to module as fast as posible after this the Frame is not valid
Result = Cam.PutFrame(Frame);
}
}
}
public void DisplayAndSaveImageFromByteArray(byte[] byteArray)
{
try{
byte[] data = new byte[width * height * 4];
int o = 0;
for (int io = 0; io < width * height; io++){
byte value = byteArray[io];
data[o++] = value;
data[o++] = value;
data[o++] = value;
data[o++] = 0;
}
unsafe
{
fixed (byte* ptr = data)
{
using (image = new Bitmap((int)width, (int)height, (int)width * 4,System.Drawing.Imaging.PixelFormat.Format32bppRgb, new IntPtr(ptr)))
{
image.Save(@"c:\\testmap\" + nextpicture + ".jpg", ImageFormat.Jpeg);
if (nextpicture >= 10)
{
pbCameraPreview.BeginInvoke(new InvokeDelegate(InvokeMethod));
}
nextpicture++;
}
}
}
}
catch (Exception ex){
MessageBox.Show(ex.ToString());
}
}
public void InvokeMethod()
{
pbCameraPreview.Image = Image.FromFile(@"c:\\testmap\" + (nextpicture -10) + ".jpg");
}
}
public class CameraCode : FireWrap_Camera
{
public FGUIntHL m_Guid;
}}
public Form1()
{
初始化组件();
pbCameraPreview.Image=defing;
}
#区域全球
int ii;
对象uu p1;
事件参数p2;
字符串路径;
Image defImg=Image.FromFile(@“c:\\testimg\def.jpg”);
UInt32宽度;
UInt32高度;
int nextpicture=0;
fgNodeInfo容器InfoContainer=新的fgNodeInfo容器();
FGNodeInfo NodeInfo=新的FGNodeInfo();
FireRap_CtrlCenter CtrlCenter;
结果;
UInt32 XSize=新UInt32();
UInt32 YSize=新UInt32();
UInt32节点;
公共委托void InvokeDelegate();
位图图像;
CameraCode Cam=新的CameraCode();
FGFrame Frame=新FGFrame();
FGUIntHL Guid=新的FGUIntHL();
#端区
公共无效检查目录()
{
路径=@“c:\\testmap\”+ii+“\\”;
if(Directory.Exists(@“c:\\testmap\”)==false)
{
目录.CreateDirectory(@“c:\\testmap\”);
}
if(目录存在(路径))
{
如果(File.Exists(路径+“0.Jpeg”))
{
ii++;
CheckDirectory();
}
}
其他的
{
CreateDirectory(路径);
}
}
//哈尔德图像作品
///
///
public void btStart\u单击(对象发送方,事件参数e)
{
Debug.WriteLine(“单击btStart_Click”);
//初始化模块
CtrlCenter=FireRap_CtrlCenter.GetInstance();
结果=CtrlCenter.FGInitModule();
//寄存器帧开始事件
CtrlCenter.OnFrameReady+=新FireRap\u CtrlCenter.FireRapEvent(OnFrameReady);
//获取已连接节点的列表
if(Result==enfirwrapresult.E_NOERROR)
{
结果=InfoContainer.fGetNodeList();
NodeCnt=InfoContainer.Size();
//打印节点
Console.WriteLine(NodeCnt.ToString()+“找到摄影机”);
//连接到第一个节点
InfoContainer.GetAt(NodeInfo,0);
结果=Cam.Connect(NodeInfo.Guid);
if(Result==enfirwrapresult.E_NOERROR)
{
Cam.m_Guid=NodeInfo.Guid;
}
//将Format7模式设置为Y8
if(Result==enfirwrapresult.E_NOERROR)
{
结果=Cam.SetParameter(enFGParameter.E_图像格式,
(uint)((uint)enFGResolution.E_RES_SCALABLE我不能100%确定我是否理解了您的问题,因为输入数组的格式不太清楚,在将其解析为位图变量之前,您必须如何对其进行格式化…但现在,我希望这些提示可以帮助您。如果没有,请尝试提供一些关于您正在尝试的内容的额外细节g我要做什么
首先,如果我理解得很好,每次将变量“value”分配给主循环中的data[o++]时,都应该增加“io”并更新变量“value”,否则将相同的值分配给R、G和B像素,这将始终导致灰色阴影
其次,我在您的代码中看到了一些不太像.net的东西…net已经提供了使用内存流等从字节数组加载图像的方法
并确保在实例化位图或图像时指示字节数组图像的正确格式-->
问候。
- 如果在StackOverflow中发布代码时遇到问题,请确保每行缩进至少4个空格(在Visual Studio中标记代码,按Tab键,然后复制),并由任何其他段落分隔至少一个空行。或者,在代码的开头和结尾处添加一行三个反勾(`)
- 当查看代码时,在我看来,您正在Windows窗体中使用第三方控件,并尝试将其事件绑定到您的某个窗口