我在从数组创建位图时遇到了几个问题。我有一个相机,从中我得到了ushort格式的灰度值。但是如何从这些值创建位图呢?仅限:
System.Drawing.Bitmap checks = new System.Drawing.Bitmap(10, 10);
.
.
checks.Save(@"C:\test.bmp", ImageFormat.Bmp);将不起作用:(.我得到了一个图像,可以用窗口工具打开它,但当我用另一个图形库打开文件时,我得到了很多错误。那么,现在有没有人知道如何创建一个正确的bmp文件的标题等?有谁有一些代码示例吗?这将是最有帮助的。
谢谢
发布于 2011-03-23 05:46:07
您应该创建一个具有正确维度(宽度、高度)的Bitmap,并使用LockBits获取应该写入的内存句柄。如果您的数据位于支持.NET的PixelFormat中,则可以将其传递给LockBits并简单地复制数据。如果没有,您可能需要手动执行一些数据转换。
这一切都归结为您接收数据样本的格式,但上面的描述概述了生成图像所需采取的步骤。
更新:由于您的数据是16位灰度,因此您可以直接使用PixelFormat PixelFormat.16bppGrayScale。
发布于 2011-03-23 06:34:00
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();
}https://stackoverflow.com/questions/5398131
复制相似问题