C#编程保存BMP格式文件
展开全部
/// <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);
}
/// 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);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询