How to put multiple images from database on one slide?

Hi,

I'm trying to put multiple images on one silde. I managed to put the images. But all the images are on top of each other at the same place. Could you please tell me how to tile them at the same size? Thanks!

The code I'm using now is:

Dim ConnStr As String = System.Configuration.ConfigurationSettings.AppSettings("ConnStr")

Dim com As New OdbcCommand

com.Connection = New OdbcConnection(ConnStr)

com.Connection.Open()

com.CommandText = "SELECT ImageID FROM ImgesTable"

com.ExecuteNonQuery()

Dim objDataReader As OdbcDataReader

objDataReader = com.ExecuteReader(CommandBehavior.CloseConnection)

While objDataReader.Read()

Dim Str As String = objDataReader("ImageID")

Dim img As System.Drawing.Image = CType(New Bitmap("\\linkpath\" & Str & ".png"), Drawing.Image)

Dim imgx As ImageEx = pres.Images.AddImage(img)

Dim idx3 As Integer = sld1.Shapes.AddPictureFrame(ShapeTypeEx.Rectangle, 20, 100, imgx.Width, imgx.Height, imgx)

Dim pf As PictureFrameEx = CType(sld1.Shapes(idx3), PictureFrameEx)

End While

Hi Jane,

Thanks for your interest in Aspose.Slides.

I have observed the code snippet shared by you. Actually, you are trying to add the shape on same position and of same size. That is why the images are getting overlapped. I have modified that code snippet shared by you which can support you to add four images at different locations on the slide. Yoyu may modify it to meet your needs.

Dim ConnStr As String = System.Configuration.ConfigurationSettings.AppSettings("ConnStr")

Dim com As New OdbcCommand

com.Connection = New OdbcConnection(ConnStr)

com.Connection.Open()

com.CommandText = "SELECT ImageID FROM ImgesTable"

com.ExecuteNonQuery()

Dim objDataReader As OdbcDataReader

objDataReader = com.ExecuteReader(CommandBehavior.CloseConnection)

Dim ImX As Integer = 20

Dim ImY As Integer = 100

Dim imgCount As Integer = 1

While objDataReader.Read()

Dim Str As String = objDataReader("ImageID")

Dim img As System.Drawing.Image = CType(New Bitmap("\\linkpath\" & Str & ".png"), Drawing.Image)

Dim imgx As ImageEx = pres.Images.AddImage(img)

Dim idx3 As Integer = sld1.Shapes.AddPictureFrame(ShapeTypeEx.Rectangle, ImX, ImY, imgx.Width, imgx.Height, imgx)

Dim pf As PictureFrameEx = CType(sld1.Shapes(idx3), PictureFrameEx)

imgCount = imgCount + 1

If imgCount = 2 Or imgCount = 4 Then

ImX = imgx.X + imgx.Width + 50

ElseIf imgCount = 3 Then ' it will shift the third image onwards to lower rows

ImX = 20

ImY = imgx.Y + imgx.Height + 50

End If

End While

Thanks and Regards,

Thanks a lot. That’s exactly what I need.

Hi,

Could you please check what went wrong in my code? For some reason, the images are on the same location again. Thanks!

Dim ConnStr As String = System.Configuration.ConfigurationSettings.AppSettings("ConnStr")

Dim com3 As New OdbcCommand

com3.Connection = New OdbcConnection(ConnStr)

com3.Connection.Open()

com3.CommandText = "SELECT ImageID..."

com3.ExecuteNonQuery()

Dim tempimageid As String = CStr(com3.ExecuteScalar())

Dim otherdiskimagesDR As OdbcDataReader

otherdiskimagesDR = com3.ExecuteReader(CommandBehavior.CloseConnection)

Dim pth As String = "E:\report\"

Dim Source As PresentationEx = New PresentationEx(pth + "Title.pptx")

Dim slds As SlidesEx = Source.Slides

Dim otherimage As PresentationEx = New PresentationEx(pth + "otherimage.pptx")

Dim otherdiskSrcSlide As SlideEx = Nothing

Dim otherdiskSlideNum As Integer = 0

Dim otherimageslide As SlideEx = otherimage.Slides(0)

otherdiskSlideNum = slds.AddClone(otherimageslide)

otherdiskSrcSlide = Source.Slides(otherdiskSlideNum)

Dim otherImgCounter As Integer = 0

While otherdiskimagesDR.Read()

Dim otherdiskimageid As String = otherdiskimagesDR("imageid")

Dim otherdiskimg As System.Drawing.Image = CType(New Bitmap("\\images\" & otherdiskimageid & ".png"), Drawing.Image)

Dim otherimgx As ImageEx = Source.Images.AddImage(otherdiskimg)

Dim ImX As Integer = 20

Dim ImY As Integer = 100

Dim idx3 As Integer = otherdiskSrcSlide.Shapes.AddPictureFrame(ShapeTypeEx.Rectangle, ImX, ImY, 200, 150, otherimgx)

Dim otherpf As PictureFrameEx = CType(otherdiskSrcSlide.Shapes(idx3), PictureFrameEx)

otherImgCounter = otherImgCounter + 1

If otherImgCounter = 2 Or otherImgCounter = 4 Then

ImX = otherimgx.X + 200 + 10

ElseIf otherImgCounter = 3 Then ' it will shift the third image onwards to lower rows

ImX = 10

ImY = otherimgx.Y + 150 + 10

End If

End While

Source.Write(pth + "imagetest.pptx")

Hi Jane,

I have observed the code snippet shared by you and i have observed that you are in fact initializing the value of ImX=20 and ImY=100 on every loop iteration. This effect reverts the changes made to ImX and ImY values that you are setting after the addition of PictureFrame to slide. The correct approach is that you must initialize the values of ImX and ImY only once and that can only be done outside scope of while loop. For your reference, I have modified the code snippet and hope it will work.

Dim ConnStr As String = System.Configuration.ConfigurationSettings.AppSettings("ConnStr")

Dim com3 As New OdbcCommand

com3.Connection = New OdbcConnection(ConnStr)

com3.Connection.Open()

com3.CommandText = "SELECT ImageID..."

com3.ExecuteNonQuery()

Dim tempimageid As String = CStr(com3.ExecuteScalar())

Dim otherdiskimagesDR As OdbcDataReader

otherdiskimagesDR = com3.ExecuteReader(CommandBehavior.CloseConnection)

Dim pth As String = "E:\report\"

Dim Source As PresentationEx = New PresentationEx(pth + "Title.pptx")

Dim slds As SlidesEx = Source.Slides

Dim otherimage As PresentationEx = New PresentationEx(pth + "otherimage.pptx")

Dim otherdiskSrcSlide As SlideEx = Nothing

Dim otherdiskSlideNum As Integer = 0

Dim otherimageslide As SlideEx = otherimage.Slides(0)

otherdiskSlideNum = slds.AddClone(otherimageslide)

otherdiskSrcSlide = Source.Slides(otherdiskSlideNum)

Dim otherImgCounter As Integer = 0

' Initialize the values here

Dim ImX As Integer = 20

Dim ImY As Integer = 100

While otherdiskimagesDR.Read()

Dim otherdiskimageid As String = otherdiskimagesDR("imageid")

Dim otherdiskimg As System.Drawing.Image = CType(New Bitmap("\\images\" & otherdiskimageid & ".png"), Drawing.Image)

Dim otherimgx As ImageEx = Source.Images.AddImage(otherdiskimg)

' You don't need to Initialize here

'Dim ImX As Integer = 20

'Dim ImY As Integer = 100

Dim idx3 As Integer = otherdiskSrcSlide.Shapes.AddPictureFrame(ShapeTypeEx.Rectangle, ImX, ImY, 200, 150, otherimgx)

Dim otherpf As PictureFrameEx = CType(otherdiskSrcSlide.Shapes(idx3), PictureFrameEx)

otherImgCounter = otherImgCounter + 1

If otherImgCounter = 2 Or otherImgCounter = 4 Then

ImX = otherimgx.X + 200 + 10

ElseIf otherImgCounter = 3 Then ' it will shift the third image onwards to lower rows

ImX = 10

ImY = otherimgx.Y + 150 + 10

End If

End While

Source.Write(pth + "imagetest.pptx")

Thanks and Regards,