请问怎么把两张图片合并为一张?

时间:2021-09-21 11:52:26
就是图片一为外框,里面透明,图片二有个图案。请教怎样把图片二放在图片一的上面,然后保存为一张新的图片。请问C#怎样实现?

35 个解决方案

#1


mark

#2


晕没玩过~~

#3


Point p1 =new Point{(0,0),(100,0),(0,100)};
point p2 = new point{(10,10),(90,10),(10,90)};

Image image1 = new Image("D:\123.jpg");
Image image2 = new Image("D:\122.jpg");

Graphics g = new Graphics(Form1);

g.Drawimage(image1,p1);
g.drawimage(image2,p2);

#4


IMAGE1、IMAGE2也是图片

//创建要显示的图片对象,并把长宽设置大一点(准备装两个图片)
System.Drawing.Image pImg3 = new System.Drawing.Bitmap(900,900);
System.Drawing.Graphics pGraphics = System.Drawing.Graphics.FromImage(pImg3);

//清除画布,背景设置为白色
pGraphics.Clear(System.Drawing.Color.White);
//在画布上画出两个图片
pGraphics.DrawImage(pImg1, 100, 100, pImg1.Width, pImg1.Height);
pGraphics.DrawImage(pImg2, 300, 100, pImg2.Width, pImg2.Height);

#5


你也可以把主画布的背景设置为TRANSPORT,让主画布透明

#6


如果想做水印,参看
http://www.codeproject.com/csharp/watermark.asp

#7


该回复被版主删除

#8


study

#9


不错

#10


做个记号,以后也许用的着

#11


我也登记下吧
...

#12


MARK

#13


Mark!

#14


也做个记号

#15


mark

#16


我写了一个组件,可以合并n张图片 自己直接引用就可以了  

组件的下载和使用说明 
可以看我的blog

http://spaces.msn.com/edwardy2418/

#17


不错的话题,做个记号,up

#18


其实和给上传的图片做水印。楼住搜索一下有很多这样的文章。

#19


Mark

#20


这个问题比较好玩 也比较郁闷 
如果是多个gif图 合成一个gif
里面的动画效果是多个效果的组合
怎么实现??

#21


Mark

#22


mark

#23


Mark

#24


mark

#25


Public Shared Function MergedImage(ByVal innerImgPath As String, ByVal outerImgPath As String, ByVal mergImgPath As String) As Boolean
        Try
            Dim innerImg As Image = Image.FromFile(innerImgPath)
            Dim outerImg As Image = Image.FromFile(outerImgPath)

            Dim b As New Bitmap(outerImg.Width, outerImg.Height, PixelFormat.Format16bppRgb555)
            Dim g As Graphics = Graphics.FromImage(b)
            g.Clear(Color.White)

            Dim p1(2) As Point
            Dim p2(2) As Point
            If outerImg.Width >= innerImg.Width Then
                If outerImg.Height >= innerImg.Height Then
                    p1(0) = New Point((outerImg.Width - innerImg.Width) \ 2, (outerImg.Height - innerImg.Height) \ 2)
                    p1(1) = New Point((outerImg.Width + innerImg.Width) \ 2, (outerImg.Height - innerImg.Height) \ 2)
                    p1(2) = New Point((outerImg.Width - innerImg.Width) \ 2, (outerImg.Height + innerImg.Height) \ 2)
                Else
                    p1(0) = New Point((outerImg.Width - innerImg.Width) \ 2, 0)
                    p1(1) = New Point((outerImg.Width + innerImg.Width) \ 2, 0)
                    p1(2) = New Point((outerImg.Width - innerImg.Width) \ 2, outerImg.Height)
                End If
            Else
                If outerImg.Height >= innerImg.Height Then
                    p1(0) = New Point(0, (outerImg.Height - innerImg.Height) \ 2)
                    p1(1) = New Point(outerImg.Width, (outerImg.Height - innerImg.Height) \ 2)
                    p1(2) = New Point(0, (outerImg.Height + innerImg.Height) \ 2)
                Else
                    p1(0) = New Point(0, 0)
                    p1(1) = New Point(outerImg.Width, 0)
                    p1(2) = New Point(0, outerImg.Height)
                End If
            End If

            p2(0) = New Point(0, 0)
            p2(1) = New Point(outerImg.Width, 0)
            p2(2) = New Point(0, outerImg.Height)

            g.DrawImage(innerImg, p1)
            g.DrawImage(outerImg, p2)

            b.Save(mergImgPath)
            outerImg.Dispose()
            innerImg.Dispose()
            Return True

        Catch ex As Exception
            Return False
        End Try

        '测试代码
        'Dim innerImgPath As String = "c:\Winter.jpg"
        'Dim outerImgPath As String = "c:\17.gif"
        'Dim savePath As String = "C:\merg.jpg"

        'If ImgMerg.MergedImage(innerImgPath, outerImgPath, savePath) = True Then
        '    Dim bMerg As New System.drawing.Bitmap(savePath)
        'Else
        '    MsgBox("错误")
        'End If
    End Function

#26


Public Shared Function MergedImage(ByVal innerImgPath As String, ByVal outerImgPath As String, ByVal mergImgPath As String) As Boolean
        Try
            Dim fi As System.IO.FileInfo = New System.IO.FileInfo(mergImgPath)
            If fi.Directory.Exists = False Then
                fi.Directory.Create()
            End If

            Dim innerImg As Image = Image.FromFile(innerImgPath)
            Dim outerImg As Image = Image.FromFile(outerImgPath)

            Dim b As New Bitmap(outerImg.Width, outerImg.Height, PixelFormat.Format16bppRgb555)
            Dim g As Graphics = Graphics.FromImage(b)
            g.Clear(Color.White)

            Dim p1(2) As Point
            Dim p2(2) As Point
            If outerImg.Width >= innerImg.Width Then
                If outerImg.Height >= innerImg.Height Then
                    p1(0) = New Point((outerImg.Width - innerImg.Width) \ 2, (outerImg.Height - innerImg.Height) \ 2)
                    p1(1) = New Point((outerImg.Width + innerImg.Width) \ 2, (outerImg.Height - innerImg.Height) \ 2)
                    p1(2) = New Point((outerImg.Width - innerImg.Width) \ 2, (outerImg.Height + innerImg.Height) \ 2)
                Else
                    p1(0) = New Point((outerImg.Width - innerImg.Width) \ 2, 0)
                    p1(1) = New Point((outerImg.Width + innerImg.Width) \ 2, 0)
                    p1(2) = New Point((outerImg.Width - innerImg.Width) \ 2, outerImg.Height)
                End If
            Else
                If outerImg.Height >= innerImg.Height Then
                    p1(0) = New Point(0, (outerImg.Height - innerImg.Height) \ 2)
                    p1(1) = New Point(outerImg.Width, (outerImg.Height - innerImg.Height) \ 2)
                    p1(2) = New Point(0, (outerImg.Height + innerImg.Height) \ 2)
                Else
                    p1(0) = New Point(0, 0)
                    p1(1) = New Point(outerImg.Width, 0)
                    p1(2) = New Point(0, outerImg.Height)
                End If
            End If

            p2(0) = New Point(0, 0)
            p2(1) = New Point(outerImg.Width, 0)
            p2(2) = New Point(0, outerImg.Height)

            g.DrawImage(innerImg, p1)
            g.DrawImage(outerImg, p2)

            b.Save(mergImgPath)
            outerImg.Dispose()
            innerImg.Dispose()
            Return True

        Catch ex As Exception
            Return False
        End Try

        '测试代码
        'Dim innerImgPath As String = "c:\Winter.jpg"
        'Dim outerImgPath As String = "c:\17.gif"
        'Dim savePath As String = "C:\merg.jpg"

        'If ImgMerg.MergedImage(innerImgPath, outerImgPath, savePath) = True Then
        '    Dim bMerg As New System.drawing.Bitmap(savePath)
        'Else
        '    MsgBox("错误")
        'End If
    End Function

#27


Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D

#28


有用,mark

#29


mark

#30


mark

#31


牛!创意无限!

#32


interesting, mark

#33


GDI+可以实现,应该和水印有类似之处

#34


mark too

#35


就是图片一为外框,里面透明,图片二有个图案。请教怎样把图片二放在图片一的上面,然后保存为一张新的图片。请问C#怎样实现?

#1


mark

#2


晕没玩过~~

#3


Point p1 =new Point{(0,0),(100,0),(0,100)};
point p2 = new point{(10,10),(90,10),(10,90)};

Image image1 = new Image("D:\123.jpg");
Image image2 = new Image("D:\122.jpg");

Graphics g = new Graphics(Form1);

g.Drawimage(image1,p1);
g.drawimage(image2,p2);

#4


IMAGE1、IMAGE2也是图片

//创建要显示的图片对象,并把长宽设置大一点(准备装两个图片)
System.Drawing.Image pImg3 = new System.Drawing.Bitmap(900,900);
System.Drawing.Graphics pGraphics = System.Drawing.Graphics.FromImage(pImg3);

//清除画布,背景设置为白色
pGraphics.Clear(System.Drawing.Color.White);
//在画布上画出两个图片
pGraphics.DrawImage(pImg1, 100, 100, pImg1.Width, pImg1.Height);
pGraphics.DrawImage(pImg2, 300, 100, pImg2.Width, pImg2.Height);

#5


你也可以把主画布的背景设置为TRANSPORT,让主画布透明

#6


如果想做水印,参看
http://www.codeproject.com/csharp/watermark.asp

#7


该回复被版主删除

#8


study

#9


不错

#10


做个记号,以后也许用的着

#11


我也登记下吧
...

#12


MARK

#13


Mark!

#14


也做个记号

#15


mark

#16


我写了一个组件,可以合并n张图片 自己直接引用就可以了  

组件的下载和使用说明 
可以看我的blog

http://spaces.msn.com/edwardy2418/

#17


不错的话题,做个记号,up

#18


其实和给上传的图片做水印。楼住搜索一下有很多这样的文章。

#19


Mark

#20


这个问题比较好玩 也比较郁闷 
如果是多个gif图 合成一个gif
里面的动画效果是多个效果的组合
怎么实现??

#21


Mark

#22


mark

#23


Mark

#24


mark

#25


Public Shared Function MergedImage(ByVal innerImgPath As String, ByVal outerImgPath As String, ByVal mergImgPath As String) As Boolean
        Try
            Dim innerImg As Image = Image.FromFile(innerImgPath)
            Dim outerImg As Image = Image.FromFile(outerImgPath)

            Dim b As New Bitmap(outerImg.Width, outerImg.Height, PixelFormat.Format16bppRgb555)
            Dim g As Graphics = Graphics.FromImage(b)
            g.Clear(Color.White)

            Dim p1(2) As Point
            Dim p2(2) As Point
            If outerImg.Width >= innerImg.Width Then
                If outerImg.Height >= innerImg.Height Then
                    p1(0) = New Point((outerImg.Width - innerImg.Width) \ 2, (outerImg.Height - innerImg.Height) \ 2)
                    p1(1) = New Point((outerImg.Width + innerImg.Width) \ 2, (outerImg.Height - innerImg.Height) \ 2)
                    p1(2) = New Point((outerImg.Width - innerImg.Width) \ 2, (outerImg.Height + innerImg.Height) \ 2)
                Else
                    p1(0) = New Point((outerImg.Width - innerImg.Width) \ 2, 0)
                    p1(1) = New Point((outerImg.Width + innerImg.Width) \ 2, 0)
                    p1(2) = New Point((outerImg.Width - innerImg.Width) \ 2, outerImg.Height)
                End If
            Else
                If outerImg.Height >= innerImg.Height Then
                    p1(0) = New Point(0, (outerImg.Height - innerImg.Height) \ 2)
                    p1(1) = New Point(outerImg.Width, (outerImg.Height - innerImg.Height) \ 2)
                    p1(2) = New Point(0, (outerImg.Height + innerImg.Height) \ 2)
                Else
                    p1(0) = New Point(0, 0)
                    p1(1) = New Point(outerImg.Width, 0)
                    p1(2) = New Point(0, outerImg.Height)
                End If
            End If

            p2(0) = New Point(0, 0)
            p2(1) = New Point(outerImg.Width, 0)
            p2(2) = New Point(0, outerImg.Height)

            g.DrawImage(innerImg, p1)
            g.DrawImage(outerImg, p2)

            b.Save(mergImgPath)
            outerImg.Dispose()
            innerImg.Dispose()
            Return True

        Catch ex As Exception
            Return False
        End Try

        '测试代码
        'Dim innerImgPath As String = "c:\Winter.jpg"
        'Dim outerImgPath As String = "c:\17.gif"
        'Dim savePath As String = "C:\merg.jpg"

        'If ImgMerg.MergedImage(innerImgPath, outerImgPath, savePath) = True Then
        '    Dim bMerg As New System.drawing.Bitmap(savePath)
        'Else
        '    MsgBox("错误")
        'End If
    End Function

#26


Public Shared Function MergedImage(ByVal innerImgPath As String, ByVal outerImgPath As String, ByVal mergImgPath As String) As Boolean
        Try
            Dim fi As System.IO.FileInfo = New System.IO.FileInfo(mergImgPath)
            If fi.Directory.Exists = False Then
                fi.Directory.Create()
            End If

            Dim innerImg As Image = Image.FromFile(innerImgPath)
            Dim outerImg As Image = Image.FromFile(outerImgPath)

            Dim b As New Bitmap(outerImg.Width, outerImg.Height, PixelFormat.Format16bppRgb555)
            Dim g As Graphics = Graphics.FromImage(b)
            g.Clear(Color.White)

            Dim p1(2) As Point
            Dim p2(2) As Point
            If outerImg.Width >= innerImg.Width Then
                If outerImg.Height >= innerImg.Height Then
                    p1(0) = New Point((outerImg.Width - innerImg.Width) \ 2, (outerImg.Height - innerImg.Height) \ 2)
                    p1(1) = New Point((outerImg.Width + innerImg.Width) \ 2, (outerImg.Height - innerImg.Height) \ 2)
                    p1(2) = New Point((outerImg.Width - innerImg.Width) \ 2, (outerImg.Height + innerImg.Height) \ 2)
                Else
                    p1(0) = New Point((outerImg.Width - innerImg.Width) \ 2, 0)
                    p1(1) = New Point((outerImg.Width + innerImg.Width) \ 2, 0)
                    p1(2) = New Point((outerImg.Width - innerImg.Width) \ 2, outerImg.Height)
                End If
            Else
                If outerImg.Height >= innerImg.Height Then
                    p1(0) = New Point(0, (outerImg.Height - innerImg.Height) \ 2)
                    p1(1) = New Point(outerImg.Width, (outerImg.Height - innerImg.Height) \ 2)
                    p1(2) = New Point(0, (outerImg.Height + innerImg.Height) \ 2)
                Else
                    p1(0) = New Point(0, 0)
                    p1(1) = New Point(outerImg.Width, 0)
                    p1(2) = New Point(0, outerImg.Height)
                End If
            End If

            p2(0) = New Point(0, 0)
            p2(1) = New Point(outerImg.Width, 0)
            p2(2) = New Point(0, outerImg.Height)

            g.DrawImage(innerImg, p1)
            g.DrawImage(outerImg, p2)

            b.Save(mergImgPath)
            outerImg.Dispose()
            innerImg.Dispose()
            Return True

        Catch ex As Exception
            Return False
        End Try

        '测试代码
        'Dim innerImgPath As String = "c:\Winter.jpg"
        'Dim outerImgPath As String = "c:\17.gif"
        'Dim savePath As String = "C:\merg.jpg"

        'If ImgMerg.MergedImage(innerImgPath, outerImgPath, savePath) = True Then
        '    Dim bMerg As New System.drawing.Bitmap(savePath)
        'Else
        '    MsgBox("错误")
        'End If
    End Function

#27


Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D

#28


有用,mark

#29


mark

#30


mark

#31


牛!创意无限!

#32


interesting, mark

#33


GDI+可以实现,应该和水印有类似之处

#34


mark too

#35


就是图片一为外框,里面透明,图片二有个图案。请教怎样把图片二放在图片一的上面,然后保存为一张新的图片。请问C#怎样实现?