我尝试从Bitmap(System.Drawing.Bitmap)获取所有字节值.因此我锁定字节并复制它们:
public static byte[] GetPixels(Bitmap bitmap){
if(bitmap-PixelFormat.Equals(PixelFormat.Format32.bppArgb)){
var argbData = new byte[bitmap.Width*bitmap.Height*4];
var bd = bitmap.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
System.Runtime.InteropServices.Marshal.Copy(bd.Scan0, argbData, 0, bitmap.Width * bitmap.Height * 4);
bitmap.UnlockBits(bd);
}
}
Run Code Online (Sandbox Code Playgroud)
我用一个非常简单的2x2 PNG图像测试了这个图像,这个图像是我在Photoshop中创建的像素(红色,绿色,蓝色,白色).由于格式,我期望argbData中的以下值:
255 255 0 0 255 0 255 0
255 0 0 255 255 255 255 255
Run Code Online (Sandbox Code Playgroud)
但我得到了:
0 0 255 255 0 255 0 255
255 0 0 255 255 255 255 255
Run Code Online (Sandbox Code Playgroud)
但这是一种BGRA格式.有人知道为什么字节似乎被交换了吗?顺便说一句,当我直接将图像用于Image.Source时,如下所示,图像显示正确.那我的错是什么?
<Image Source="D:/tmp/test2.png"/>
Run Code Online (Sandbox Code Playgroud) 除了这个问题,我还有另一个问题.我尝试从外部进程获取二进制数据,但数据(图像)似乎已损坏.下面的屏幕截图显示了损坏:左图像是通过在命令行执行程序完成的,右边是代码.
我的代码到目前为止:
var process = new Process
{
StartInfo =
{
Arguments = string.Format(@"-display"),
FileName = configuration.PathToExternalSift,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
},
EnableRaisingEvents = true
};
process.ErrorDataReceived += (ProcessErrorDataReceived);
process.Start();
process.BeginErrorReadLine();
//Reads in pbm file.
using (var streamReader = new StreamReader(configuration.Source))
{
process.StandardInput.Write(streamReader.ReadToEnd());
process.StandardInput.Flush();
process.StandardInput.Close();
}
//redirect output to file.
using (var fileStream = new FileStream(configuration.Destination, FileMode.OpenOrCreate))
{
process.StandardOutput.BaseStream.CopyTo(fileStream);
}
process.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
这是某种编码问题吗?我使用了这里提到的Stream.CopyTo-Approach 来避免出现问题.
我有一个源列表,看起来像:
let source = ["A", "B", "%", "C", "Y", "%"]
Run Code Online (Sandbox Code Playgroud)
我想浏览每个元素,每次点击标记"%"时,前面列表中的每个元素都应该进入子列表.结果应该是这样的.
let result = [["A", "B"], ["C", "Y"]]
Run Code Online (Sandbox Code Playgroud)
我想我必须使用list的fold函数,但我的结果类型是字符串列表而不是字符串列表
let folder (acc, current) item =
match item with
| "" -> (current @ acc, [])
| _ -> (acc, current @ [item])
let result = source
|> List.fold folder ([], [])
|> fun (a,_) -> a
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?