将位图复制到新位图,保持文件大小和位深度相同:C#
我开发了一个Windows服务应用程序。当我在本地测试它时,一切都很完美,但当我把文件放在服务器上时,一切都失败了 应用程序正在从传真服务器获取tiff文件,一个文件可以有多个页面。应用程序获取一个文件并将其拆分,以获取文件中的所有页面,并将其保存到数据库中 问题是位图,我必须将位图图像复制到新位图,如果我将其保存到原始位图,应用程序将失败。当我将其保存到新位图时,文件大小会改变,它会变大,位深度从1变为32。我希望它保持不变 我从我的朋友谷歌那里得到了一个解决方案,它将位图从32转换为1,这个解决方案的问题是有时它不会复制文件的某些部分 我的最终目标是能够将位图保存到Memorystream,并且我不希望更改文件大小 我正在将文件保存为PNG。JPG和TIFF格式工作完美。我想使用PNG,因为当我将文件保存为PNG时,与JPG和TIFF相比,文件大小非常小 这是我的密码将位图复制到新位图,保持文件大小和位深度相同:C#,c#,system.drawing,C#,System.drawing,我开发了一个Windows服务应用程序。当我在本地测试它时,一切都很完美,但当我把文件放在服务器上时,一切都失败了 应用程序正在从传真服务器获取tiff文件,一个文件可以有多个页面。应用程序获取一个文件并将其拆分,以获取文件中的所有页面,并将其保存到数据库中 问题是位图,我必须将位图图像复制到新位图,如果我将其保存到原始位图,应用程序将失败。当我将其保存到新位图时,文件大小会改变,它会变大,位深度从1变为32。我希望它保持不变 我从我的朋友谷歌那里得到了一个解决方案,它将位图从32转换为1,这个
public static Bitmap GetImage(Bitmap image, int index, ImageFormat imageFormat, ref byte[] imageStream)
{
Bitmap newImage=null;
try
{
int imageWidth = int.Parse(ConfigurationManager.AppSettings["ImageWidth"]);
int imageHeight = int.Parse(ConfigurationManager.AppSettings["ImageHeight"]);
if (image == null) return null;
image.SelectActiveFrame(FrameDimension.Page, index);
newImage = new Bitmap(image);
newImage = BitmapTo1Bpp(newImage); // i do not want to use this method its not reliable. sometimes it does not copy some parts of the file
using (var byteStream = new MemoryStream())
{
//image.Save(byteStream,imageFormat) this one if failing on the server.
newImage.Save(byteStream, imageFormat); // the file size is changing it becomes big.
imageStream = byteStream.ToArray();
}
return newImage;
}
catch (Exception ex)
{
Logger.Error(ex.ToString(), "Class : " + MethodBase.GetCurrentMethod().DeclaringType.FullName + ", Method : " + MethodBase.GetCurrentMethod().Name);
return null;
}
}
public static Bitmap BitmapTo1Bpp(Bitmap img)
{
int w = img.Width;
int h = img.Height;
Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
try
{
BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
byte[] scan = new byte[(w + 7) / 8];
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
if (x % 8 == 0) scan[x / 8] = 0;
Color c = img.GetPixel(x, y);
if (c.GetBrightness() >= 0.5) scan[x / 8] |= (byte)(0x80 >> (x % 8));
}
Marshal.Copy(scan, 0, (IntPtr)((long)data.Scan0 + data.Stride * y), scan.Length);
}
bmp.UnlockBits(data);
}
catch (Exception ex)
{
Logger.Error(ex.ToString(), "Class : " + MethodBase.GetCurrentMethod().DeclaringType.FullName + ", Method : " + MethodBase.GetCurrentMethod().Name);
return null;
}
return bmp;
}
公共静态位图GetImage(位图图像、int索引、ImageFormat ImageFormat、ref byte[]imageStream)
{
位图newImage=null;
尝试
{
int imageWidth=int.Parse(ConfigurationManager.AppSettings[“imageWidth”]);
int imageHeight=int.Parse(ConfigurationManager.AppSettings[“imageHeight”]);
if(image==null)返回null;
image.SelectActiveFrame(FrameDimension.Page,index);
newImage=新位图(图像);
newImage=BitmapTo1Bpp(newImage);//我不想使用此方法,因为它不可靠。有时它不会复制文件的某些部分
使用(var byteStream=newmemorystream())
{
//如果在服务器上出现故障,请保存(byteStream,imageFormat)此文件。
newImage.Save(byteStream,imageFormat);//文件大小正在改变,它变大了。
imageStream=ByTestStream.ToArray();
}
返回新图像;
}
捕获(例外情况除外)
{
错误(例如,ToString(),“类:”+MethodBase.GetCurrentMethod().DeclaringType.FullName+”,方法:“+MethodBase.GetCurrentMethod().Name”);
返回null;
}
}
公共静态位图BitmapTo1Bpp(位图img)
{
int w=最小宽度;
int h=img.高度;
位图bmp=新位图(w、h、PixelFormat.Format1BPindexed);
尝试
{
BitmapData data=bmp.LockBits(新矩形(0,0,w,h),ImageLockMode.ReadWrite,PixelFormat.Format1BPindexed);
字节[]扫描=新字节[(w+7)/8];
对于(int y=0;y=0.5)扫描[x/8]|=(字节)(0x80>>(x%8));
}
Marshal.Copy(扫描,0,(IntPtr)((长)data.Scan0+data.Stride*y),扫描.长度);
}
解锁位(数据);
}
捕获(例外情况除外)
{
错误(例如,ToString(),“类:”+MethodBase.GetCurrentMethod().DeclaringType.FullName+”,方法:“+MethodBase.GetCurrentMethod().Name”);
返回null;
}
返回bmp;
}
是否尝试使用位图的克隆实现
克隆似乎创建了一个具有相同精确大小、像素格式和所有剩余设置的克隆
class Program
{
static void Main(string[] args)
{
Bitmap b = Bitmap.FromFile(Path.Combine(Environment.CurrentDirectory, "test.bmp")) as Bitmap;
Bitmap b2 = b.Clone() as Bitmap;
Console.WriteLine("Height: original: {0}, clone: {1}",b.Size.Height, b2.Size.Height);
Console.WriteLine("Width: original: {0}, clone: {1}",b.Size.Width, b2.Size.Width);
}
}
您是否尝试使用位图的克隆实现 克隆似乎创建了一个具有相同精确大小、像素格式和所有剩余设置的克隆
class Program
{
static void Main(string[] args)
{
Bitmap b = Bitmap.FromFile(Path.Combine(Environment.CurrentDirectory, "test.bmp")) as Bitmap;
Bitmap b2 = b.Clone() as Bitmap;
Console.WriteLine("Height: original: {0}, clone: {1}",b.Size.Height, b2.Size.Height);
Console.WriteLine("Width: original: {0}, clone: {1}",b.Size.Width, b2.Size.Width);
}
}
您是否尝试使用位图的克隆实现 克隆似乎创建了一个具有相同精确大小、像素格式和所有剩余设置的克隆
class Program
{
static void Main(string[] args)
{
Bitmap b = Bitmap.FromFile(Path.Combine(Environment.CurrentDirectory, "test.bmp")) as Bitmap;
Bitmap b2 = b.Clone() as Bitmap;
Console.WriteLine("Height: original: {0}, clone: {1}",b.Size.Height, b2.Size.Height);
Console.WriteLine("Width: original: {0}, clone: {1}",b.Size.Width, b2.Size.Width);
}
}
您是否尝试使用位图的克隆实现 克隆似乎创建了一个具有相同精确大小、像素格式和所有剩余设置的克隆
class Program
{
static void Main(string[] args)
{
Bitmap b = Bitmap.FromFile(Path.Combine(Environment.CurrentDirectory, "test.bmp")) as Bitmap;
Bitmap b2 = b.Clone() as Bitmap;
Console.WriteLine("Height: original: {0}, clone: {1}",b.Size.Height, b2.Size.Height);
Console.WriteLine("Width: original: {0}, clone: {1}",b.Size.Width, b2.Size.Width);
}
}
您还可以尝试通过GDI+的
DrawImage
克隆图像:
Bitmap clone = new Bitmap(srcWidth, srcHeight, srcPixelFormat );
Graphics g = Graphics.FromImage(clone);
Rectangle srcRect = new Rectangle(0, 0, srcWidth, srcHeight);
Rectangle dstRect = new Rectangle(0, 0, srcWidth, srcHeight);
g.DrawImage(bitmap, dstRect, srcRect, GraphicsUnit.Pixel);
如果以下值影响图像大小,则可以使用这些值(以下通常是复制精确图像的设置):
您还可以尝试通过GDI+的
DrawImage
克隆图像:
Bitmap clone = new Bitmap(srcWidth, srcHeight, srcPixelFormat );
Graphics g = Graphics.FromImage(clone);
Rectangle srcRect = new Rectangle(0, 0, srcWidth, srcHeight);
Rectangle dstRect = new Rectangle(0, 0, srcWidth, srcHeight);
g.DrawImage(bitmap, dstRect, srcRect, GraphicsUnit.Pixel);
如果以下值影响图像大小,则可以使用这些值(以下通常是复制精确图像的设置):
您还可以尝试通过GDI+的
DrawImage
克隆图像:
Bitmap clone = new Bitmap(srcWidth, srcHeight, srcPixelFormat );
Graphics g = Graphics.FromImage(clone);
Rectangle srcRect = new Rectangle(0, 0, srcWidth, srcHeight);
Rectangle dstRect = new Rectangle(0, 0, srcWidth, srcHeight);
g.DrawImage(bitmap, dstRect, srcRect, GraphicsUnit.Pixel);
如果以下值影响图像大小,则可以使用这些值(以下通常是复制精确图像的设置):
您还可以尝试通过GDI+的
DrawImage
克隆图像:
Bitmap clone = new Bitmap(srcWidth, srcHeight, srcPixelFormat );
Graphics g = Graphics.FromImage(clone);
Rectangle srcRect = new Rectangle(0, 0, srcWidth, srcHeight);
Rectangle dstRect = new Rectangle(0, 0, srcWidth, srcHeight);
g.DrawImage(bitmap, dstRect, srcRect, GraphicsUnit.Pixel);
如果以下值影响图像大小,则可以使用这些值(以下通常是复制精确图像的设置):
我试图克隆位图。它在本地工作,但我在服务器上运行它失败了。克隆不工作吗?或者属性不保持相同?很明显克隆在位图上不起作用,结果是
FromFile
我试图克隆位图。它在本地工作,但我在服务器上运行它失败了。克隆不工作吗?或者属性不保持相同?很明显克隆在位图上不起作用,结果是FromFile
我试图克隆位图。它在本地工作,但我在服务器上运行它失败了。克隆不工作吗?或者属性不保持相同?很明显克隆在位图上不起作用,结果是FromFile
我试图克隆位图。它在本地工作,但我在服务器上运行它失败了。克隆不工作吗?或者属性不保持相同