public Bitmap SetResolution(Bitmap bitmap)
{
if ((bitmap.Height < 512) && (bitmap.Width < 1024))
{
return bitmap;
}
Rectangle rec = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData bmpData = bitmap.LockBits(rec, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr Scan0 = bmpData.Scan0;
int stride = bmpData.Stride;
int nWidth = bmpData.Width;
int nHeight = bmpData.Height;
int size = bitmap.Width / 512 - 1;
int width = nWidth / size;
int height = nHeight / size;
Bitmap z_Bitmap = new Bitmap(width, height);
Rectangle rec2 = new Rectangle(0, 0, z_Bitmap.Width, z_Bitmap.Height);
BitmapData bmpData2 = z_Bitmap.LockBits(rec2, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr Scan02 = bmpData2.Scan0;
int nWidth2 = bmpData2.Width;
int nHeight2 = bmpData2.Height;
unsafe
{
byte* p = (byte*)(void*)Scan0;
byte* p2 = (byte*)(void*)Scan02;
for (int i = 0; i < nHeight2; i++)
{
for (int j = 0; j < nWidth2; j++)
{
p2[0] = p[0];
p2[1] = p[1];
p2[2] = p[2];
// p += 3;
p += size * 3;
p2 += 3;
}
p += stride - size * 3 * nWidth2;
p2 += bmpData2.Stride - nWidth2 * 3; p += stride * (size - 1); }
}
bitmap.UnlockBits(bmpData);
z_Bitmap.UnlockBits(bmpData2);
return z_Bitmap;
}为什么变换后的图片保存后,PS不认为它是图片。而且在C++程序也不识别。Bitmap图片
{
if ((bitmap.Height < 512) && (bitmap.Width < 1024))
{
return bitmap;
}
Rectangle rec = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData bmpData = bitmap.LockBits(rec, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr Scan0 = bmpData.Scan0;
int stride = bmpData.Stride;
int nWidth = bmpData.Width;
int nHeight = bmpData.Height;
int size = bitmap.Width / 512 - 1;
int width = nWidth / size;
int height = nHeight / size;
Bitmap z_Bitmap = new Bitmap(width, height);
Rectangle rec2 = new Rectangle(0, 0, z_Bitmap.Width, z_Bitmap.Height);
BitmapData bmpData2 = z_Bitmap.LockBits(rec2, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr Scan02 = bmpData2.Scan0;
int nWidth2 = bmpData2.Width;
int nHeight2 = bmpData2.Height;
unsafe
{
byte* p = (byte*)(void*)Scan0;
byte* p2 = (byte*)(void*)Scan02;
for (int i = 0; i < nHeight2; i++)
{
for (int j = 0; j < nWidth2; j++)
{
p2[0] = p[0];
p2[1] = p[1];
p2[2] = p[2];
// p += 3;
p += size * 3;
p2 += 3;
}
p += stride - size * 3 * nWidth2;
p2 += bmpData2.Stride - nWidth2 * 3; p += stride * (size - 1); }
}
bitmap.UnlockBits(bmpData);
z_Bitmap.UnlockBits(bmpData2);
return z_Bitmap;
}为什么变换后的图片保存后,PS不认为它是图片。而且在C++程序也不识别。Bitmap图片
openBitmap我先赋了值的
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Bitmap文件(*.bmp)|*.bmp| Jpeg文件(*.jpg)|*.jpg| 所有合适文件(*.bmp/*.jpg)|*.bmp/*.jpg";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
if (DialogResult.OK == saveFileDialog.ShowDialog())
{
openBitmap.Save(saveFileDialog.FileName);
}
}
private void btnSave_Click(object sender, EventArgs e)
{
Bitmap openBitmap = null;
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Bitmap文件(*.bmp)|*.bmp| Jpeg文件(*.jpg)|*.jpg| Png文件(*.png)|*.png";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
if (DialogResult.OK == saveFileDialog.ShowDialog())
{
switch (saveFileDialog.FilterIndex)
{
case 2:
openBitmap.Save(saveFileDialog.FileName, ImageFormat.Jpeg);
break;
case 3:
openBitmap.Save(saveFileDialog.FileName, ImageFormat.Png);
break;
default:
openBitmap.Save(saveFileDialog.FileName, ImageFormat.Bmp);
break;
}
}
}