Hack Forums
[.NET] Drawing on Custom image control - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Drawing on Custom image control (/showthread.php?tid=3619504)



Drawing on Custom image control - Doomsapian - Jul 13, 2013

So I'm using this custom image control which allows me to pan and zoom really large images easily (5000px * 5000px) but I can't draw on the image no matter what I do.

Custom Image Control

Somewhere in the comments the creator says to add:

Code
Public Event PaintOverlay(ByVal g As Graphics)

and put this in the onPaint event
Code
RaiseEvent PaintOverlay(e.Graphics)

Then add a handler to that to draw on. This doesn't work.
All I have is:
Code
Private Sub Map_Paint(ByVal g As Graphics)
g.DrawString("Test string", Font_Calibri, Brush_Red, 50, 50)
End Sub
I have tested Map_Paint is getting called, which it is, but no drawing :(

Heres the code for the entire control (plus a few edits by me):
Spoiler (Click to View)
More specifically this is the paint event in the control from above:
Code
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  e.Graphics.Clear(Me.BackColor)
  DrawImage(e.Graphics)
  RaiseEvent PaintOverlay(e.Graphics)
  MyBase.OnPaint(e)
    End Sub



* - C3ol - Jul 14, 2013

Code
Dim img As Image = Image.FromFile(location)
    Dim image1 As Image = New Bitmap(img.Width, img.Height)
    Dim G As Graphics = Graphics.FromImage(image1)
    G.DrawImage(img, New Rectangle(0, 0, image1.Width, image1.Height), 0, 0, image1.Width, image1.Height, GraphicsUnit.Pixel)
    img.Dispose()
    G.DrawString(Str, New Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel), New SolidBrush(Color.FromArgb(75, 0, 0, 0)), New Point(x, y))
    G.Dispose()



* - Meerkat. - Jul 14, 2013

Perhaps you can just use the paint event instead of the custom one?


* - Doomsapian - Jul 14, 2013

(Jul 14, 2013 06:26 AM)Mavamaarten~ Wrote:  Perhaps you can just use the paint event instead of the custom one?
If you mean just adding a handler to the custom control for paint, that doesn't work. I tried it but I think (specifically because it says Protected Overrides) because of the one in the control, it screws it up.

If you mean putting my code inside the paint event in the control class, I would have to make about a billion variables and functions public which would cause all kinds of problems.


* - Meerkat. - Jul 14, 2013

(Jul 14, 2013 06:44 AM)The Inspector Wrote:  If you mean just adding a handler to the custom control for paint, that doesn't work. I tried it but I think (specifically because it says Protected Overrides) because of the one in the control, it screws it up.

If you mean putting my code inside the paint event in the control class, I would have to make about a billion variables and functions public which would cause all kinds of problems.

I meant just using the paint event like this:
[Image: Z5SufSb.png]

It should work as the paint event (as shown above) is calledafter drawing everything inside the control.


* - Doomsapian - Jul 14, 2013

(Jul 14, 2013 12:56 PM)Mavamaarten~ Wrote:  I meant just using the paint event like this:
[Image: Z5SufSb.png]

It should work as the paint event (as shown above) is calledafter drawing everything inside the control.
I have it slightly different but it should be same none the less.

I declare the custom image control in the code and initilize it that way instead of actually adding it on the form in the designer. Then I just add a handler to it for the paint event, but it doesn't work.

PHP Code

<?php 
Public Map As New DrawingBoard

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
  
'Create Map
  Map.Size = New Size(1280, 1280)
  Map.Image = My.Resources.map
  AddHandler Map.Paint, AddressOf Map_Paint
  NsTheme1.Controls.Add(Map)
End Sub

Private Sub Map_Paint(ByVal g As Graphics)
g.DrawString("Test string", Font_Calibri, Brush_Red, 50, 50)
End Sub 
That should be near identical to what your saying, except instead of having the custom image control in the designer, I'm adding it with code.
This doesn't work. In the comments of the control on the website, the guy who made it says its a problem he has to look into, but clearly never has.

http://www.codeproject.com/Articles/15743/Pan-and-Zoom-Very-Large-Images?msg=2620311#xx2620311xx


Ok I had to do this in a new post because everytime I tried to edit my previous message some cloud flare thing popped up telling me to enter a captcha that didn't exist.

I got it working anyway, no idea how. Now I just gotta figure out how to get the drawn images to stick to the main image when it pans and zooms around.

Think I'm just going have to throw this control out the door, seems the way it draws the image makes it impossible for me to draw anything on the image and keep it where I want it!


* - Meerkat. - Jul 15, 2013

You can just draw on the Image that is inside the Image property? (From the top of my head:)
Code
Dim G as graphics = Graphics.FromImage(PicBox.Image)
G.DrawStuff



* - Doomsapian - Jul 15, 2013

Mava. What would I do without you, you magnificent beast. My only problem is that every thing that I draw looks like total shit.

[Image: B9IxB84.jpg]

vs Original

[Image: DnnV3jS.jpg]

Two very different images. The original has transparency in it and I think thats getting destroyed. Not sure whats going on with the sizing though.

And I've just gone back to my old problem of the image drawing itself a million times because the Imagebox is constantly invalidated. Like the idea is the image is meant to move, but not create a million copies of itself.

Working with graphics is just not fun in anyway.


* - Meerkat. - Jul 16, 2013

(Jul 15, 2013 10:53 PM)The Inspector Wrote:  Mava. What would I do without you, you magnificent beast. My only problem is that every thing that I draw looks like total shit.

[Image: B9IxB84.jpg]

vs Original

[Image: DnnV3jS.jpg]

Two very different images. The original has transparency in it and I think thats getting destroyed. Not sure whats going on with the sizing though.

And I've just gone back to my old problem of the image drawing itself a million times because the Imagebox is constantly invalidated. Like the idea is the image is meant to move, but not create a million copies of itself.

Working with graphics is just not fun in anyway.

Ah, I think you're drawing the image many times on top of itself. That's why it looks so weird.
Perhaps you can just "refresh" the image every time you draw on it? (G.Clear(), G.DrawImage("Original image"), G.DrawImage("the transparent thingy"))


* - Doomsapian - Jul 16, 2013

Using that method, works, but makes it very laggy and slow.

Memory usage goes up about 4x as well.

Its just never easy is it.

I'm thinking something like using the background image property to display my main image, then draw on the image property with my things, but have no idea how to implement this. I think I would have to rewrite some of that control.

Or what about 2 controls overlapping each other one with drawn stuff, the other with the image and they both pan and zoom at the same time.

Yeah I think I really need some sort of overlay layer I can draw to, rather then drawing on the big image.

I don't see a way to implement this though.


* - ᒪeumonic - Jul 16, 2013

Try double buffering your control and see if that helps at all.