0

I have a picturebox as the main image and it has a filled rectangle drawn onto it. Then I have another picturebox which has an image and a label which has some text. I want to draw the second picturebox's image and the label's text onto the first picturebox's image and display it in a third picturebox.

I have the following code:

Dim BMP As New Bitmap(PicBanner.Image)
    Dim g As Graphics = Graphics.FromImage(BMP)
    Dim Brsh As New SolidBrush(Color.FromName(CBForeClr.SelectedItem))
    g.DrawImageUnscaled(PicLogo.Image, PicLogo.Location.X, PicLogo.Location.Y)
    g.DrawString(LblText.Text, LblText.Font, Brsh, LblText.Left, LblText.Top)
    PicBanner.DrawToBitmap(BMP, New Rectangle(0, 0, PicPreview.Width, PicPreview.Height))
    PicPreview.Image = BMP

Instead of drawing the picturebox's image and the text onto the first picturebox, it displays only the filled rectangle in the preview box.

What is wrong with my code?

Leeuwenhok
  • 13
  • 2
  • 7
  • @valter PicLogo is the image I want to paste onto PicBanner and show the result in PicPreview. Btw, I just noticed that the code works fine but it doesn't show the printed image and label because it is beyond the bounds of the PicPreview as it's location is set to PicLogo.Location. I want to paste the label and image on the PicPreview at the same position as their location is on the PicBanner. Actually the app allows the user to move the label and PicLogo with the mouse and adjust their position on top of PicBanner. – Leeuwenhok Mar 30 '14 at 09:39
  • I managed to do it by this code: g.DrawString(LblText.Text, LblText.Font, Brsh, LblText.Location.X - PicBanner.Location.X, LblText.Location.Y - PicBanner.Location.Y) g.DrawImageUnscaled(PicLogo.Image, PicLogo.Location.X - PicBanner.Location.X, PicLogo.Location.Y - PicBanner.Location.Y) – Leeuwenhok Mar 30 '14 at 09:44
  • Post your solution as answer and mark it as solved. – γηράσκω δ' αεί πολλά διδασκόμε Mar 30 '14 at 09:45
  • @valter I've got another problem now. The PicLogo (image to be pasted) is set to Zoom property and when the it is pasted onto the PicPreview, it scales up to original size. How do I keep it the same size as the PicLogo picturebox? – Leeuwenhok Mar 30 '14 at 09:54
  • 1
    Don't use `DrawImageUnscaled` but `DrawImage` and define the size in this call. See here: http://msdn.microsoft.com/de-de/library/yws82c40.aspx – Jens Mar 30 '14 at 09:54
  • @Jens Thanks, that worked! Now one more thing... Is there a way to select which should be pasted first, the image or the test? Currently the image is on top of the text in the generated pic. Or can it be fixed by simply typing the text paste code before the pic paste code? – Leeuwenhok Mar 30 '14 at 10:16
  • Yes. Think of the graphics object as a paint canvas. You draw one thing on it after another. So start with the bottom layer and work your way up. – Jens Mar 30 '14 at 10:50
  • @Jens If I set the image size (to be pasted) to the PicLogo's image size, then the image scales up but if I set the size to PicLogo's size, then the image appears stretched. How do I paste the image the way it looks in the PicLogo? – Leeuwenhok Mar 30 '14 at 15:56
  • Zoom-Mode means the Aspect Ratio is kept the same. You need to calculate the reduced size with that aspect ratio. See here: http://stackoverflow.com/questions/3971841/how-to-resize-images-proportionally-keeping-the-aspect-ratio it's javascript, but shouldn't be hard to get the idea. – Jens Mar 30 '14 at 16:49

0 Answers0