簡體   English   中英

在背景之上插入一系列較小的位圖 bitmap

[英]Insert series of smaller bitmaps on top of background bitmap

這可能是一個業余問題,但我仍然被困住了......

我有一張bitmap的背景圖,然后需要疊加幾個小一點的位圖(主要是二維碼)。 事情適用於第一個插入,然后它會中斷。 它編譯正常,但在新的 Bitmap 行上失敗,並顯示異常未處理消息 System.ArgumentException:“參數無效”。

代碼是這樣的

        Bitmap Background_bmp= new Bitmap(File_name);
        Graphics Background_gfx = Graphics.FromImage(Background_bmp);

        for (i=1;i<=4;i++)
        {
            Bitmap Insert_image = new Bitmap(File_name[i]); 
            Print_doc_gfx.DrawImage(Insert_image, blablabla (scaling and positioning);
            Insert_image.Dispose();
        }

        Background_bmp.Save("C:\\Total image.bmp");
        Background_gfx.Dispose();
        Background_bmp.Dispose();

很簡單,但它不起作用。 我很確定破損是在“新位圖”片段中重復的“新”,但我不知道如何聲明一次並多次使用位圖......就像我說的,業余問題...

您從代碼中發布的部分似乎沒有引起問題。 這很可能是由代碼的其他部分引起的,例如圖像插入的坐標或完全不同的東西。

我使用以下代碼對一張大圖像和 4 張小圖像進行了測試,並且沒有問題:

string File_name = "background.png";
string[] File_names = new string[] { "img1.png", "img2.png", "img3.png", "img4.png" };
Bitmap Background_bmp = new Bitmap(File_name);
// can also use empty all-black image like the following line:
// Bitmap Background_bmp = new Bitmap(800, 600, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
         
Graphics Background_gfx = Graphics.FromImage(Background_bmp);

for (int i = 1; i <= File_names.Length; i++)
{
   Bitmap Insert_image = new Bitmap(File_names[i - 1]);
   Background_gfx.DrawImage(Insert_image, i * 150, i * 100);
   Insert_image.Dispose();
}

Background_gfx.Dispose();
Background_bmp.Save("Total_image.png", System.Drawing.Imaging.ImageFormat.Png);
Background_bmp.Dispose();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM