การผสานสองภาพใน C # /. NET


87

แนวคิดง่ายๆ: ฉันมีสองภาพที่ต้องการรวมภาพหนึ่งคือ 500x500 ที่โปร่งใสตรงกลางอีกภาพหนึ่งคือ 150x150

แนวคิดพื้นฐานมีดังนี้: สร้างผืนผ้าใบว่างเปล่าที่มีขนาด 500x500 วางภาพ 150x150 ไว้ตรงกลางของผืนผ้าใบที่ว่างเปล่าแล้วคัดลอกภาพ 500x500 ไปทับเพื่อให้ตรงกลางโปร่งใสช่วยให้ 150x150 ส่องผ่าน

ฉันรู้วิธีทำใน Java, PHP และ Python ... ฉันไม่รู้เลยว่าจะใช้วัตถุ / คลาสอะไรใน C # ตัวอย่างสั้น ๆ ของการคัดลอกรูปภาพไปยังอื่นก็เพียงพอแล้ว


สิ่งนี้ช่วยได้หรือไม่? daniweb.com/forums/thread87993.html
Dror

คำตอบ:


99

โดยพื้นฐานแล้วฉันใช้สิ่งนี้ในหนึ่งในแอพของเรา: เราต้องการวาง playicon บนเฟรมของวิดีโอ:

Image playbutton;
try
{
    playbutton = Image.FromFile(/*somekindofpath*/);
}
catch (Exception ex)
{
    return;
}

Image frame;
try
{
    frame = Image.FromFile(/*somekindofpath*/);
}
catch (Exception ex)
{
    return;
}

using (frame)
{
    using (var bitmap = new Bitmap(width, height))
    {
        using (var canvas = Graphics.FromImage(bitmap))
        {
            canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
            canvas.DrawImage(frame,
                             new Rectangle(0,
                                           0,
                                           width,
                                           height),
                             new Rectangle(0,
                                           0,
                                           frame.Width,
                                           frame.Height),
                             GraphicsUnit.Pixel);
            canvas.DrawImage(playbutton,
                             (bitmap.Width / 2) - (playbutton.Width / 2),
                             (bitmap.Height / 2) - (playbutton.Height / 2));
            canvas.Save();
        }
        try
        {
            bitmap.Save(/*somekindofpath*/,
                        System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (Exception ex) { }
    }
}

10
ขอบคุณ! วันนี้เก็บเบคอนของฉันไว้หมดแล้ว
Jason More

@downvoter ดูแลอย่างละเอียดเพื่อที่ฉันจะได้เพิ่มคำตอบของฉัน?
Andreas Niedermair

5
@AndreasNiedermair ผู้มีสิทธิเลือกตั้งอาจคัดลอกวางรหัสของคุณและใช้งานไม่ได้
Jean-Paul

มันคือคำตอบที่เป็นทอง!
DmitryBoyko

60

สิ่งนี้จะเพิ่มรูปภาพไปยังอีกภาพหนึ่ง

using (Graphics grfx = Graphics.FromImage(image))
{
    grfx.DrawImage(newImage, x, y)
}

กราฟิกอยู่ในเนมสเปซ System.Drawing


35

หลังจากนี้ฉันก็พบวิธีใหม่ที่ง่ายกว่านี้ลองทำดู ..

สามารถรวมภาพถ่ายหลายภาพเข้าด้วยกัน:

public static System.Drawing.Bitmap CombineBitmap(string[] files)
{
    //read all images into memory
    List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
    System.Drawing.Bitmap finalImage = null;

    try
    {
        int width = 0;
        int height = 0;

        foreach (string image in files)
        {
            //create a Bitmap from the file and add it to the list
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);

            //update the size of the final bitmap
            width += bitmap.Width;
            height = bitmap.Height > height ? bitmap.Height : height;

            images.Add(bitmap);
        }

        //create a bitmap to hold the combined image
        finalImage = new System.Drawing.Bitmap(width, height);

        //get a graphics object from the image so we can draw on it
        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
        {
            //set background color
            g.Clear(System.Drawing.Color.Black);

            //go through each image and draw it on the final image
            int offset = 0;
            foreach (System.Drawing.Bitmap image in images)
            {
                g.DrawImage(image,
                  new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
                offset += image.Width;
            }
        }

        return finalImage;
    }
    catch (Exception ex)
    {
        if (finalImage != null)
            finalImage.Dispose();

        throw ex;
    }
    finally
    {
        //clean up memory
        foreach (System.Drawing.Bitmap image in images)
        {
            image.Dispose();
        }
    }
}

5
ทำงานได้ดี g.Clear (Color.Transparent) หากคุณต้องการผสานภาพ PNG สำหรับสไปรต์แอนิเมชั่น
syclee

1
finalImage = System.Drawing.Bitmap ใหม่ (ความกว้างความสูง); แสดงข้อผิดพลาดสำหรับค่าความกว้าง / ความสูงที่สูง
zeetit

@Anant Dabhi เอาล่ะฉันขอโทษที่ต้องนำคำถามเก่า ๆ กลับมา แต่ฉันแปลงเป็น VB.NET .. สิ่งนี้จะซ้อนทับภาพถ่ายอื่น ๆ หรือไม่ถ้าฉันวางซ้อนทับกันถ้าพิกเซลที่ไม่ได้ใช้ / พิกเซลว่างในภาพถัดไปโปร่งใสหรือไม่? ถ้าไม่ได้มีวิธีใดบ้าง?
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.