我使用以下内容将a转换BitmapSource为Bitmap:
internal static Bitmap ConvertBitmapSourceToBitmap(BitmapSource bitmapSrc)
{
int width = bitmapSrc.PixelWidth;
int height = bitmapSrc.PixelHeight;
int stride = width * ((bitmapSrc.Format.BitsPerPixel + 7) / 8);
byte[] bits = new byte[height * stride];
bitmapSrc.CopyPixels(bits, stride, 0);
unsafe
{
fixed (byte* pBits = bits)
{
IntPtr ptr = new IntPtr(pBits);
return new System.Drawing.Bitmap(
width,
height,
stride,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb, //The problem
ptr);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何得到PixelFormat它BitmapSource,所以我的图像被破坏了.
对于上下文,我使用这种技术,因为我想加载一个tiff,可能是8或16灰色或24或32位颜色,我需要PixelFormat保留.我更喜欢修复ConvertBitmapSourceToBitmap它,因为它相当方便,但也很乐意用更好的技术替换以下代码,从BitmapSource创建一个Bitmap:
Byte[] buffer = File.ReadAllBytes(filename.FullName);
using …Run Code Online (Sandbox Code Playgroud) 更新的问题更通用:
我有以下代码.当您交换线程[i] .Join()的位置时,您会得到不同的输出.
static void ThreadedWorker(int startIndex, int endIndex)
{
Console.WriteLine("Working from results[ " + startIndex +"] to results["+endIndex+"]");
}
static void Main(string[] args)
{
int threadCount = System.Environment.ProcessorCount;
int calculationCount = 500; //the number of array elements we'd be iterating over if we were doing our work
int threadDataChunkSize = calculationCount / threadCount;
if (threadDataChunkSize < 1) threadDataChunkSize = 1; //just in case we have loads of threads
Thread[] threads = new Thread[threadCount];
for (int i = 0; i …Run Code Online (Sandbox Code Playgroud)