0

从数组创建位图有几个问题。我有一台相机,从中我得到了 ushort 格式的灰度值。但是如何从这些值创建位图?仅有的:

System.Drawing.Bitmap checks = new System.Drawing.Bitmap(10,  10);
.
.
checks.Save(@"C:\test.bmp", ImageFormat.Bmp);

不会工作:(。我得到一个图像,可以用窗口工具打开它,但是当我用另一个图形库打开文件时,我得到了很多错误。所以现在有人如何创建一个正确的带有标题等的 bmp 文件? 有人有一些代码示例吗?这将最有帮助。

谢谢

4

2 回答 2

2

您应该创建Bitmap具有正确尺寸(宽度、高度)的 a,并使用它LockBits来获取您应该写入的内存句柄。如果您的数据采用 .NET 支持的 PixelFormat,您可以将其传递给 LockBits 并简单地复制数据。如果没有,您可能需要手动进行一些数据转换。

这一切都归结为您接收数据样本的格式,但上面的描述概述了生成图像所需的步骤。

更新:由于您的数据是 16 位灰度,因此PixelFormat您可以直接使用PixelFormat.16bppGrayScale.

于 2011-03-22T21:46:06.643 回答
0
public class(path,wid,height,boolean)
{ 

System.Drawing.Image myThumbnail150;
            System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            System.Drawing.Image imagesize = System.Drawing.Image.FromFile(pic.FilePath);
            using (Bitmap bitmapNew = new Bitmap(imagesize))
            {

            double maxWidth = Convert.ToDouble(ConfigurationSettings.AppSettings["ImageWidth"]);
            double maxHeight = Convert.ToDouble(ConfigurationSettings.AppSettings["ImageHeight"]);
            int w = imagesize.Width;
            int h = imagesize.Height;
            // Longest and shortest dimension 
            int longestDimension = (w > h) ? w : h;
            int shortestDimension = (w < h) ? w : h;
            // propotionality  
            float factor = ((float)longestDimension) / shortestDimension;
            // default width is greater than height    
            double newWidth = maxWidth;
            double newHeight = maxWidth / factor;
            // if height greater than width recalculate  
            if (w < h)
            {
                newWidth = maxHeight / factor;
                newHeight = maxHeight;
            }
            myThumbnail150 = bitmapNew.GetThumbnailImage((int)newWidth, (int)newHeight, myCallback, IntPtr.Zero);
            string name = pic.Name.Replace(Path.GetExtension(pic.Name), ".Bmp");

            //Create a new directory name ThumbnailImage
            //Save image in TumbnailImage folder
            myThumbnail150.Save(yourpath+ name, System.Drawing.Imaging.ImageFormat.Bmp);
            bitmapNew.Dispose();
}
于 2011-03-22T22:34:00.180 回答