C# 如何使用Graphics类的DrawImage方法将大图像分割成更小的A4大小的片段

C# 如何使用Graphics类的DrawImage方法将大图像分割成更小的A4大小的片段,c#,graphics,C#,Graphics,Graphics类的DrawImage方法不能创建高质量的图像。在这种方法中,我将主图像分割为多个图像,但生成第一个图像的代码质量非常低。但它正在生成剩余高度的全黑色图像 public static Bitmap[] Split(byte[] ByteImage) { // MasterImage: there is no problem in master image. it is saving it in good quality.

Graphics类的DrawImage方法不能创建高质量的图像。在这种方法中,我将主图像分割为多个图像,但生成第一个图像的代码质量非常低。但它正在生成剩余高度的全黑色图像

     public static Bitmap[] Split(byte[] ByteImage)
            {
    // MasterImage:  there is no problem in master image. it is saving it in good quality.
                MemoryStream ms = new MemoryStream(ByteImage);

                System.Drawing.Image MasterImage = System.Drawing.Image.FromStream(ms);

                MasterImage.Save(HttpContext.Current.Server.MapPath("../../../App_Shared/Reports/Temp/MasterImage.Bmp"), ImageFormat.Bmp);


    //Split master image into multiple image according to height / 1000
                Int32 ImageHeight = 1000, ImageWidth = MasterImage.Width, MasterImageHeight = MasterImage.Height;
                int PageCount = 0;
                Int32 TotalPages = MasterImage.Height / 1000;
                Bitmap[] imgs = new Bitmap[TotalPages];

                for (int y = 0; y + 1000 < MasterImageHeight; y += 1000, PageCount++)
                {
                    imgs[PageCount] = new Bitmap(ImageWidth, ImageHeight, PixelFormat.Format32bppPArgb);

                    using (Graphics gr = Graphics.FromImage(imgs[PageCount]))
                    {
                        gr.CompositingQuality = CompositingQuality.HighQuality;
                        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        gr.SmoothingMode = SmoothingMode.HighQuality;
//First image now working with this code line
 gr.DrawImage(MasterImage, new System.Drawing.Rectangle(0, y, ImageWidth, ImageHeight),new System.Drawing.Rectangle(0, 0, ImageWidth, ImageHeight), GraphicsUnit.Pixel); //new System.Drawing.Rectangle(new Point(0, y), new Size(ImageWidth, ImageHeight)));
                        //gr.DrawImage(MasterImage, new System.Drawing.Rectangle(0, y, ImageWidth, ImageHeight)); //new System.Drawing.Rectangle(new Point(0, y), new Size(ImageWidth, ImageHeight)));

                        string FilePath = HttpContext.Current.Server.MapPath("../../../App_Shared/Reports/Temp/Image" + PageCount.ToString() + ".bmp");
                        imgs[PageCount].Save(FilePath, System.Drawing.Imaging.ImageFormat.Bmp);
    //Here it is saving images. I got first image with very poor quality but remaining in total balck color.
                        gr.Dispose();
                    }
                }

                return imgs;
            }
公共静态位图[]拆分(字节[]ByteImage)
{
//主映像:主映像没有问题。它以良好的质量保存它。
MemoryStream ms=新的MemoryStream(ByteImage);
System.Drawing.Image MasterImage=System.Drawing.Image.FromStream(毫秒);
MasterImage.Save(HttpContext.Current.Server.MapPath(“../../../App\u Shared/Reports/Temp/MasterImage.Bmp”)、ImageFormat.Bmp);
//根据高度/1000将主图像拆分为多个图像
Int32 ImageHeight=1000,ImageWidth=MasterImage.Width,MasterImageHeight=MasterImage.Height;
int PageCount=0;
Int32 TotalPages=主图像高度/1000;
位图[]imgs=新位图[TotalPages];
对于(int y=0;y+1000

正如@HansPassant提到的,源矩形和目标矩形是反向的

您还可以稍微更改拆分的结构,使其工作更加灵活,并且在以后可能具有更好的可读性

class Program
{
    static IList<Bitmap> SplitImage(Bitmap sourceBitmap, int splitHeight)
    {
        Size dimension = sourceBitmap.Size;
        Rectangle sourceRectangle = new Rectangle(0, 0, dimension.Width, splitHeight);
        Rectangle targetRectangle = new Rectangle(0, 0, dimension.Width, splitHeight);

        IList<Bitmap> results = new List<Bitmap>();

        while (sourceRectangle.Top < dimension.Height)
        {
            Bitmap pageBitmap = new Bitmap(targetRectangle.Size.Width, sourceRectangle.Bottom < dimension.Height ?
                targetRectangle.Size.Height
                :
                dimension.Height - sourceRectangle.Top, PixelFormat.Format32bppArgb);

            using (Graphics g = Graphics.FromImage(pageBitmap))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                g.DrawImage(sourceBitmap, targetRectangle, sourceRectangle, GraphicsUnit.Pixel);
            }
            sourceRectangle.Y += sourceRectangle.Height;
            results.Add(pageBitmap);
        }

        return results;
    }

    static void Main(string[] args)
    {
        string sourceFilename = Environment.CurrentDirectory + @"\testimage.jpg";
        Bitmap sourceBitmap = (Bitmap)Image.FromFile(sourceFilename);

        var images = SplitImage(sourceBitmap, 79);
        int len = images.Count;
        for (int x = len; --x >= 0; )
        {
            var bmp = images[x];
            string filename = "Images-" + x + ".bmp";
            bmp.Save(Environment.CurrentDirectory + @"\" + filename, ImageFormat.Bmp);
            images.RemoveAt(x);
            bmp.Dispose();
            Console.WriteLine("Saved " + filename);
        }
        Console.WriteLine("Done with the resizing");
    }
}
类程序
{
静态IList SplitImage(位图源位图,int splitHeight)
{
Size-dimension=sourceBitmap.Size;
矩形sourceRectangle=新矩形(0,0,dimension.Width,splitHeight);
矩形targetRectangle=新矩形(0,0,维度.宽度,拆分高度);
IList results=新列表();
while(sourceRectangle.Top=0;)
{
var bmp=图像[x];
字符串filename=“Images-”+x+“.bmp”;
保存(Environment.CurrentDirectory+@“\”+文件名,ImageFormat.bmp);
图像。移除(x);
bmp.Dispose();
Console.WriteLine(“已保存”+文件名);
}
Console.WriteLine(“完成了大小调整”);
}
}

如果页面在结尾处小于指定的位图高度,这也会动态调整最后一幅图像的大小:)

能否在问题中添加前后图像,显示发生了什么?链接已失效。编辑..NM这是我的防火墙…您在DrawImage()调用中反转了矩形。第一个应该是(0,0,…),第二个应该是(0,y,…)。所以你所拥有的只适用于y=0。没有任何关于质量的提示,源内容使用非常小的字体,只有简单的抗锯齿。只有当你看得不太近的时候才好看。就像您在原始图像上看到的一样,它太大,无法容纳您的显示器,因此会被缩小。感谢您提供的解决方案和额外功能“动态调整最后一张图像的大小”