Home > Net >  Know the first address, height, width, how to save the grayscale images
Know the first address, height, width, how to save the grayscale images

Time:10-16

RT, image format is 8-bit grayscale images, now know that it's the first address, height, width, and how to obtain its gray image

CodePudding user response:

Calculate a byte [] size, 8-bit grayscale said 1 1 byte

Byte [] buffer=new byte [width * height]

Marshal. Copy (first address, buffer, 0, buffer length);//we don't know, what is the first address, I assume it is hosting the address, so using Marshal. Copy

CodePudding user response:

Byte [] arr=your data. The Skip (first address). The ToArray ();
Image img=Image. FromStream (new MemoryStream (arr));

CodePudding user response:

reference 1st floor wanghui0380 response:
calculate a byte [] size, 8-bit grayscale said 1 1 byte

Byte [] buffer=new byte [width * height]

Marshal. Copy (first address, buffer, 0, buffer length);//we don't know, what is the first address, I assume it is hosting the address, so using Marshal. Copy
can speak more specifically?

CodePudding user response:

reference danran251689 reply: 3/f
Quote: refer to 1st floor wanghui0380 response:

Calculate a byte [] size, 8-bit grayscale said 1 1 byte

Byte [] buffer=new byte [width * height]

Marshal. Copy (first address, buffer, 0, buffer length);//we don't know, what is the first address, I assume it is hosting the address, so using Marshal. Copy
can speak more specifically?


Specific, we don't know you specific use environment, can only assume that this is the result of calling a c + + DLL, so may be returned to you the width, height and an address

Such as return inptr p, int width, int,

8-bit grayscale image, it is a byte (eight bits) represents a pixel, so how many pixels width * height can have, namely the number of bytes,

Marshal. Copy means how many bytes of data read from the memory to the specified managed array

Explain about this thing to see the MSDN
https://docs.microsoft.com/zh-cn/dotnet/api/system.runtime.interopservices.marshal.copy? Redirectedfrom=MSDN& View=netframework 4.8 # overloads

CodePudding user response:

reference 4 floor wanghui0380 response:
Quote: reference danran251689 reply: 3/f

Quote: refer to 1st floor wanghui0380 response:

Calculate a byte [] size, 8-bit grayscale said 1 1 byte

Byte [] buffer=new byte [width * height]

Marshal. Copy (first address, buffer, 0, buffer length);//we don't know, what is the first address, I assume it is hosting the address, so using Marshal. Copy
can speak more specifically?


Specific, we don't know you specific use environment, can only assume that this is the result of calling a c + + DLL, so may be returned to you the width, height and an address

Such as return inptr p, int width, int,

8-bit grayscale image, it is a byte (eight bits) represents a pixel, so how many pixels width * height can have, namely the number of bytes,

Marshal. Copy means how many bytes of data read from the memory to the specified managed array

Explain about this thing to see the MSDN
https://docs.microsoft.com/zh-cn/dotnet/api/system.runtime.interopservices.marshal.copy? Redirectedfrom=MSDN& View=netframework 4.8 # overloads

This image is displayed by other controls, now I can get to the image of the first address Scan0, width and height and format (Format8bppIndexed), now want to save images into the depth of 8 bits BMP format,

CodePudding user response:

 byte [and] Data;//save the pixel matrix 
Int width_=0;
Int heigth_=0;
Int chnum_=0;
Long buff_=0;
Int tool=axCKVisionCtrl1. GetTool (" gathering image ");
Buff_=axCKVisionCtrl1. GetImageBuffer (tool, ref width_, ref heigth_, ref chnum_);
Bitmap BMP=new Bitmap (width_, heigth_);
//Lock the bitmap 's bits.
BitmapData bmpData=https://bbs.csdn.net/topics/bmp.LockBits (new Rectangle (0, 0, BMP. Width, BMP, Height), ImageLockMode. ReadOnly, PixelFormat. Format24bppRgb);
This. Width=bmpData. Width;
This. Height=bmpData. Height;
Data=https://bbs.csdn.net/topics/new byte (Height, Width),
The unsafe
{
Byte * PTR=(byte *) buff_;
for (int i=0; i {
For (int j=0; j {
//convert the 24-bit RGB color chart to grayscale
Int temp=(int) (0.114 * (* ptr++)) + (int) (0.587 * (* ptr++)) + (int) (0.299 * (* ptr++));
Data [I, j)=temp (byte);
}
PTR +=bmpData. Stride - Width * 3;//pointer and fill the blank space
}
}
BMP. UnlockBits (bmpData);
PictureBox1. Image=BMP;

My code as shown above, width_ for access to the width of the height_ for access to highly, buff_ for access to the image of the first address, now run by "int temp=(int) (0.114 * (* ptr++)) + (int) (0.587 * (* ptr++)) + (int) (0.299 * (* ptr++));" This sentence is error, prompt "" System. AccessViolationException" type of untreated abnormalities in WindowsFormsApplication1. Exe in

Other information: trying to read or write protected memory, this usually indicates other memory has been damaged,"
  •  Tags:  
  • C#