Assembler 2 images sous vb.net

Résolu
cs_DAN1 Messages postés 78 Date d'inscription samedi 12 avril 2003 Statut Membre Dernière intervention 19 juillet 2017 - 8 nov. 2010 à 10:45
NHenry Messages postés 15112 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 13 avril 2024 - 17 nov. 2010 à 20:54
Bonjour a tous .
pour assembler 2 images sous vb.net j'ai trouvé le code suivant qui fonctionne correctement mais le processus est un peu long.
je cherche donc une autre méthode plus simple et plus rapide la méthode drawing m'a t'on dit .
je cherche pour cela un petit bout de code ou un exemple.
Si quelqu'un pouvai m'aider je lui en serai trés reconaissant.
Voici le code que j'utilise ,qui fonctionnerais que je trouve trop long.

Dim img1 As Bitmap = Image.FromFile("C:\ToughWalkingOne.png")
Dim img2 As Bitmap = Image.FromFile("C:\ToughStanding.png")

Dim newImage As New Bitmap(img1.Width + img2.Width, img1.Height)

For i As Integer = 0 To img1.Width - 1
For j As Integer = 0 To img1.Height - 1
newImage.SetPixel(i, j, img1.GetPixel(i, j))
Next
Next

For i As Integer = 0 To img2.Width - 1
For j As Integer = 0 To img2.Height - 1
newImage.SetPixel(i + img1.Width, j, img2.GetPixel(i, j))
Next
Next

newImage.Save("C:\new.png")

9 réponses

cs_DAN1 Messages postés 78 Date d'inscription samedi 12 avril 2003 Statut Membre Dernière intervention 19 juillet 2017 3
14 nov. 2010 à 18:21
Merci beaucoup ca marche bien mais un petit pb c'est que j'ai du multiplier par 2 les hauteures et les largeures pour eviter que les 2 images de memes tailles se chevauchent et soient tronquées sur la hauteur,d'ou cela pourai bien provenir selon toi?

Wide = 2 * Pic1.Width + pic2.Width
et ....
If pic1.Height >= Pic2.Height Then
High = 2 * Pic1.Height
Else
High = 2 * pic2.Height
End If
1
NHenry Messages postés 15112 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 13 avril 2024 159
8 nov. 2010 à 12:53
Bonjour,

Cette méthode a l'air mieux (et gère la transparence) :
http://blogs.vbcity.com/xtab/archive/2006/09/21/6213.aspx

Ensuite, à toi d'adapter.

0
raffika Messages postés 269 Date d'inscription dimanche 31 mai 2009 Statut Membre Dernière intervention 12 mars 2011
8 nov. 2010 à 13:16
Salut,
tu peux utiliser DrawImage pour dessiner
un rectangle de la taille que tu veux contenant
une image :

Dim img1 As Bitmap = Image.FromFile("C:\ToughWalkingOne.png")
Dim img2 As Bitmap = Image.FromFile("C:\ToughStanding.png")

Dim newImage As New Bitmap(img1.Width + img2.Width, img1.Height)

Dim NewGraphic As Graphics = PictureBox1.CreateGraphics()
NewGraphic.DrawImage(img1, 1, 1, img1.width, img1.height)
''Dessine img1 dont les coordonnées du coin en haut à gauche est 1;1
''d'un rectangle de la taille img1.width;img1.height
''(sur un Picturebox1)
'For i As Integer = 0 To img1.Width - 1
'For j As Integer = 0 To img1.Height - 1
'newImage.SetPixel(i, j, img1.GetPixel(i, j))
'Next
'Next

NewGraphic.DrawImage(img2, img1.width, img1.height, img2.width, img2.height)
''Pareil pour l'image 2 -> coordonnées du coin gauche à img1.width;img1.height
''de taille img2.width;img2.height , toujours sur le Picturebox1
'For i As Integer = 0 To img2.Width - 1
'For j As Integer = 0 To img2.Height - 1
'newImage.SetPixel(i + img1.Width, j, img2.GetPixel(i, j))
'Next
'Next

Je n'ai pas testé mon code mais cela devrai marcher,
peut être qu'il y aura un décalage de 1 pixel sur l'emplacement
ou la taille de l'image.

Salutations
0
raffika Messages postés 269 Date d'inscription dimanche 31 mai 2009 Statut Membre Dernière intervention 12 mars 2011
8 nov. 2010 à 13:24
PS : Pour le deuxième DrawImage :
NewGraphic.DrawImage(img2, img1.Width, 1, img2.Width, img2.Height)
est plus juste. Salut
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_DAN1 Messages postés 78 Date d'inscription samedi 12 avril 2003 Statut Membre Dernière intervention 19 juillet 2017 3
8 nov. 2010 à 20:15
merci beaucoupc'est super sympa, je cherchais depuis un bon bout de temps.
Je mimai dès que possible et je vous tiens au courant.
Encore merci qui
0
cs_DAN1 Messages postés 78 Date d'inscription samedi 12 avril 2003 Statut Membre Dernière intervention 19 juillet 2017 3
14 nov. 2010 à 14:56
Bonjour.
J'ai testé le code du site:
http://blogs.vbcity.com/xtab/archive/2006/09/21/6213.aspx
après qq difficultés je n'ai plus d'erreurs, j'ai 2 picture box pic1 et pic2.
maintenant je désire afficher le résultat de la fusion dans une troisième piqûre box nommée pic3.
je bute sur l'instruction :
pic3.image = gr.????
car je pense que c'est bien dans GR que se trouve le résultat.
Pourriez-vous me donner un dernier coup de main ?
D'avance merci pour votre réponse
José cliquets ce que je fais pour le moment.
Dans une forme j'ai donc décidé mes trois piqûres box pic1 pic2 et pic3
dans pic1 et pic2 et j'ai mis deux images.
Voici le code :

Imports System.IO.Path
Imports VB = Microsoft.VisualBasic
Imports System.Windows.Forms.ListBox
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO.Directory
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fs As System.IO.FileStream

Dim zzS = "E:\VBESSAIPICTUREBOX\VBESSAIPICTURE\VBESSAIPICTURE\bin\Debug\DATA\A.jpg"
fs = New System.IO.FileStream(zzS, IO.FileMode.Open, IO.FileAccess.Read)
pic1.Image = System.Drawing.Image.FromStream(fs)
fs.Dispose()
fs = Nothing
ajust(pic1, Panel1)

Dim zst = "E:\VBESSAIPICTUREBOX\VBESSAIPICTURE\VBESSAIPICTURE\bin\Debug\DATA\B.jpg"

' charger image dans picturebox3


' Specify a valid picture file path on your computer.
fs = New System.IO.FileStream(zst, IO.FileMode.Open, IO.FileAccess.Read)
pic2.Image = System.Drawing.Image.FromStream(fs)
fs.Dispose()
fs = Nothing
ajust(pic2, Panel2)
' MergeImage(zss, zst)
' MergeImage(pic1, pic2)
' End Sub
' Public Function MergeImages(ByVal Pic1 As Image, ByVal pic2 As Image) As Image


Dim MergedImage As Image ' This will be the finished merged image



Dim Wide, High As Integer ' Size of merged image

' Calculate Width and Height needed for composite image

' First, the Width:

Wide = pic1.Width + pic2.Width



' Height: Ensure that the new image is high enough for both images

' that we plan to place inside it.

If pic1.Height >= pic2.Height Then

High = pic1.Height

Else

High = pic2.Height

End If



' Create an empty Bitmap the correct size to hold both images side by side

Dim bm As New Bitmap(Wide, High)

' Get the Graphics object for this bitmap

Dim gr As Graphics = Graphics.FromImage(bm)



' Draw a black line round the outside (optional, but sometimes looks better when printed)

gr.DrawRectangle(Pens.Black, 0, 0, Wide - 1, High - 1)

' Draw the first source image at left side of new image


gr.DrawImage(pic1.Image, 0, 0)

' Draw second source image, offset to the right edge of first source image

gr.DrawImage(pic2.Image, pic1.Width, 0)



' Assign the merged bitmap you have just created as the image

' you are going to return for printing

MergedImage = bm



' Finished with the Graphics object – dispose of it

gr.Dispose()



' You now have an Image named MergedImage which you can print.

' Return MergedImage

pic3.image = ????????

End Sub

Public Sub ajust(ByVal virt, ByVal pan)
Dim haut = pan.Height
Dim larg = pan.Width
virt.SizeMode = PictureBoxSizeMode.Zoom
If (virt.Width > larg) Or (virt.Height > haut) Then

virt.Width = virt.Width / (virt.Width / larg)
virt.Height = virt.Height / (virt.Height / haut)
End If
If (virt.Width < larg) Or (virt.Height < haut) Then

virt.Width = virt.Width * (larg / virt.Width)
virt.Height = virt.Height * (haut / virt.Height)

End If
End Sub

End Class
0
cs_DAN1 Messages postés 78 Date d'inscription samedi 12 avril 2003 Statut Membre Dernière intervention 19 juillet 2017 3
14 nov. 2010 à 15:16
Salut raffika .
de retour d'un petit séjour à Metz (c'est une très belle ville et le musée magnifique) j'ai testé la réponse que tu m'avais.

ta methode fonctionne bien,c'est celle que j'utilise pour le moment, malheureusement elle a le défaut d'être trop longue, c'est justement pour cela que j'ai posté sur le forum.
La méthode que me propose NHenry semble beaucoup plus rapide malheureusement je bute sur la récupération du résultat dans une les Picture box.
0
NHenry Messages postés 15112 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 13 avril 2024 159
14 nov. 2010 à 15:59
Bonjour,

"Dans une forme j'ai donc décidé mes trois piqûres box pic1 pic2 et pic3 "
piqûres box ????

Public Function MergeImages(ByVal Pic1 As Image, ByVal pic2 As Image) As Image
 
Dim MergedImage As Image &#8216; This will be the finished merged image
 
Dim Wide, High As Integer ' Size of merged image
' Calculate Width and Height needed for composite image
' First, the Width:
Wide = Pic1.Width + pic2.Width
 
' Height: Ensure that the new image is high enough for both images
' that we plan to place inside it.
   If Pic1.Height >= pic2.Height Then
     High = Pic1.Height
   Else
     High = pic2.Height
   End If
 
' Create an empty Bitmap the correct size to hold both images side by side
Dim bm As New Bitmap(Wide, High)
' Get the Graphics object for this bitmap
Dim gr As Graphics = Graphics.FromImage(bm)
 
' Draw a black line round the outside (optional, but sometimes looks better when printed)
gr.DrawRectangle(Pens.Black, 0, 0, Wide - 1, High - 1)
' Draw the first source image at left side of new image
gr.DrawImage(Pic1, 0, 0)
' Draw second source image, offset to the right edge of first source image
gr.DrawImage(pic2, Pic1.Width, 0)
 
' Assign the merged bitmap you have just created as the image
 ' you are going to return for printing
MergedImage = bm
 
' Finished with the Graphics object &#8211; dispose of it
gr.Dispose()
 
' You now have an Image named MergedImage which you can print.
Return MergedImage
 
End Function


'Dans ton appel :
Pic3.Image=MergeImage(Pict.Image,Pic2.Image)


0
NHenry Messages postés 15112 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 13 avril 2024 159
17 nov. 2010 à 20:54
Bonjour,

Non, je ne vois pas pourquoi, dsl.

0
Rejoignez-nous