C#编程保存BMP格式文件

C#编程,比如将一个二维数组中的数据按顺序显示,然后将显示的内容保存为一个图片,怎么样进做?... C#编程,比如将一个二维数组中的数据按顺序显示,然后将显示的内容保存为一个图片,怎么样进做? 展开
 我来答
wangziyi129
2008-12-24 · TA获得超过1387个赞
知道小有建树答主
回答量:1492
采纳率:100%
帮助的人:0
展开全部
/// <summary>
/// Clone image
/// Note: It looks like Bitmap.Clone() with specified PixelFormat does not
/// produce expected result
/// </summary>
public static Bitmap Clone(Bitmap src, PixelFormat format)
{
// copy image if pixel format is the same
if (src.PixelFormat == format)
return Clone(src);

int width = src.Width;
int height = src.Height;

// create new image with desired pixel format
Bitmap bmp = new Bitmap(width, height, format);

// draw source image on the new one using Graphics
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(src, 0, 0, width, height);
g.Dispose();

return bmp;
}
// and with unspecified PixelFormat it works strange too
public static Bitmap Clone(Bitmap src)
{
// get source image size
int width = src.Width;
int height = src.Height;

// lock source bitmap data
BitmapData srcData = src.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite, src.PixelFormat);

// create new image
Bitmap dst = new Bitmap(width, height, src.PixelFormat);

// lock destination bitmap data
BitmapData dstData = dst.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite, dst.PixelFormat);

Win32.memcpy(dstData.Scan0, srcData.Scan0, height * srcData.Stride);

// unlock both images
dst.UnlockBits(dstData);
src.UnlockBits(srcData);

//
if ((src.PixelFormat == PixelFormat.Format1bppIndexed) ||
(src.PixelFormat == PixelFormat.Format4bppIndexed) ||
(src.PixelFormat == PixelFormat.Format8bppIndexed) ||
(src.PixelFormat == PixelFormat.Indexed))
{
ColorPalette srcPalette = src.Palette;
ColorPalette dstPalette = dst.Palette;

int n = srcPalette.Entries.Length;

// copy pallete
for (int i = 0; i < n; i++)
{
dstPalette.Entries[i] = srcPalette.Entries[i];
}

dst.Palette = dstPalette;
}

return dst;
}

/// <summary>
/// Windows API functions and structures
/// </summary>
internal class Win32
{
// memcpy - copy a block of memory
[DllImport("ntdll.dll")]
public static extern IntPtr memcpy(
IntPtr dst,
IntPtr src,
int count);
[DllImport("ntdll.dll")]
public static extern unsafe byte* memcpy(
byte* dst,
byte* src,
int count);

// memset - fill memory with specified values
[DllImport("ntdll.dll")]
public static extern IntPtr memset(
IntPtr dst,
int filler,
int count);
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式