How to put image over another one by reading/setting pixels ? so the readed pixels will be part of the other image.

Chocolade 536 Reputation points
2023-01-07T00:12:17.08+00:00

This is after merging two images. the result is those clouds overlay the other image. but it looks like the clouds are above the other image and not part of the other image.

277103-radar-without-clouds-overlay.jpg

but this is what i want to do. in the end this is the logic of what i want to get but that the pixels to be part of the other image.
here are the two images as they are by original.

The first image is the image i want to read the pixels and put over the other one type PNG 940x940 Bit depth 8
on my hard disk the background is white and here it's black but this is the clouds i want to get and put on the other image.

277064-202301062155.png

and this image is the image i want to put the pixels on :

277083-radar-without-clouds.jpg

I created this class for reading and setting the pixels using the two images the problem is that the image with the clouds the pixels i'm reading have a white background so it's reading this pixels too so the result is like the image with the clouds it's not reading only the clouds and put them on the other image.

this is the result using the class :

277092-mynewbmp1.jpg

using System;  
using System.Collections.Generic;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace Weather  
{  
    public class RadarPixels  
    {  
        public RadarPixels(Bitmap image1, Bitmap image2)  
        {  
            ReadSetPixels(image1, image2);  
        }  
  
        private void ReadSetPixels(Bitmap image1 , Bitmap image2)  
        {  
            for(int x = 0; x < image1.Width; x++)  
            {  
                for(int y = 0; y < image1.Height; y++)  
                {  
                    Color pixelColor = image1.GetPixel(x, y);  
                    image2.SetPixel(x, y, pixelColor);  
                }  
            }  
  
            image2.Save(@"d:\mynewbmp.bmp");  
  
            image1.Dispose();  
            image2.Dispose();  
        }  
    }  
}  

using it :

public Form1()  
        {  
            InitializeComponent();  
  
            RadarPixels rp = new RadarPixels(  
                new Bitmap(Image.FromFile(@"D:\1.png")),  
                new Bitmap(Image.FromFile(@"D:\2.jpg")));  
        }  
Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2023-01-08T21:27:00.837+00:00

    You can use MakeTransparent

    A test with your 2 images and 3 PictureBoxes + 1 Button :

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        Dim image1 As System.Drawing.Image = System.Drawing.Image.FromFile("E:\1.png")  
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage  
        PictureBox1.Image = image1  
    
        Dim image2 As System.Drawing.Image = System.Drawing.Image.FromFile("E:\2.jpg")  
        PictureBox2.SizeMode = PictureBoxSizeMode.StretchImage  
        PictureBox2.Image = image2  
    
        PictureBox3.SizeMode = PictureBoxSizeMode.StretchImage  
    End Sub  
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        If True Then  
            Dim bitmap1 As Bitmap = CType(PictureBox1.Image, Bitmap)  
            Dim bitmap2 As Bitmap = CType(PictureBox2.Image, Bitmap)  
            bitmap1.MakeTransparent(Color.White)  
            Dim bmp As New Bitmap(bitmap2.Width, bitmap2.Height)  
            Using gr = Graphics.FromImage(bmp)  
                gr.DrawImage(bitmap2, New Rectangle(0, 0, bitmap2.Width, bitmap2.Height), 0, 0, bitmap2.Width, bitmap2.Height, GraphicsUnit.Pixel, Nothing)  
                gr.DrawImage(bitmap1, New Rectangle(0, 0, bitmap1.Width, bitmap1.Height), 0, 0, bitmap1.Width, bitmap1.Height, GraphicsUnit.Pixel, Nothing)  
                If (PictureBox3.Image IsNot Nothing) Then  
                    PictureBox3.Image.Dispose()  
                End If  
                PictureBox3.Image = bmp  
            End Using  
            PictureBox3.Refresh()  
    
            Return  
        End If   
    End Sub  
    

    277273-merge.jpg


1 additional answer

Sort by: Most helpful
  1. Ken Tucker 5,861 Reputation points
    2023-01-08T13:32:05.177+00:00

    I would use Graphics.DrawImage for this

            var backgroundImage = Image.FromFile("277083-radar-without-clouds.jpg");  
            var cloudOverlay = Image.FromFile("clouds.png");  
            var g = Graphics.FromImage(backgroundImage);  
            g.DrawImage(cloudOverlay, 0, 0, backgroundImage.Width, backgroundImage.Height);  
    
            this.BackgroundImageLayout = ImageLayout.Stretch;  
            this.BackgroundImage = new Bitmap(backgroundImage);  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.