I toying with a project where I can position objects runtime on the screen. I also have an small "preview"-window, where I want to see the outline of the objects.
I have an object array who have size and location on the "scene". Now I want to loop through my scene-objects and create one picturebox in the preview-window for each object in the "scene".
So, I have the size of the "Scene" and an array of the objects that are visible on the scene. Every object have an size and location mapped to the scene.
I also have a preview window that have an dynamic size. How do I create a preview that scales down to the preview window? To make it easy, I loop through the scene objects in the scene and create a picturebox on the preview-form for each object, that uses the preview-forms size to do the scaling.
SceneSize=new size(800,600)
PreviewSize=new size(80,60)
For each obj as SceneObject in Scene
dim p as new picturebox
p.size=?? scaling here (we have obj.size and obj.location)
p.location=?? scaling here (we have obj.size and obj.location)
previewWindow.controls.add (p)
next
Any example in .Net would be nice.
Thanks.
Bob Powell's site on GDI+ might have some useful information on how to proceed. Check out his page on Matrix Transformations. This article might provide you with some good ideas on how to proceed
Bob Powell GDI+ Transformations
This did the trick, it wasn't harder than that.
Function GetPreviewRectangleForObject(ByVal ScreenSize As Size, ByVal PreviewSize As Size, ByVal OriginalRect As Rectangle) As Rectangle
Dim xFactor As Integer = ScreenSize.Width / PreviewSize.Width
Dim yFactor As Integer = ScreenSize.Height / PreviewSize.Height
With OriginalRect
Return New Rectangle(.X / xFactor, .Y / yFactor, .Width / xFactor, .Height / yFactor)
End With
End Function
Related
I have a Picturebox which I draw a view to (Gantt View in this case) and it works OK - i.e., the view is drawn and the AutoScroll property allows the image in the PictureBox to be smoothly scrolled.
My problem is, the header of the image (e.g., the date headers in this case) scroll off the top of the display when I scroll down the image.
What I can't work out is how to fix a header to the top. I thought about simply drawing a header into another Picturebox, but then I am not sure how to sync the header with the left-right scrolling of the main PictureBox
Can someone suggest the best approach to handling this, or do I need to revert to doing a direct draw and handle the scrolling myself?
I am using VB with VS 2015.
Many thanks
Phil
Updated - I am now using an off-screen Bitmap, but can someone look at the code below and let me know if there is a faster/better way to do this? It all works, but still learning and so always looking to do things the best way
Public Sub MoveViewPoint(G As Graphics)
' G passed in from controls Paint
G.Clear(Color.WhiteSmoke)
' _Plan is off-screen bitmap of image
' _HeaderHeight is height of the Header area in _Plan
Dim Header_src_rect As New Rectangle(_HScroll.Value, 0, _Plan.Width, _HeaderHeight)
Dim Header_dst_rect As New Rectangle(0, 0, _Plan.Width, _HeaderHeight)
G.DrawImage(_Plan, Header_dst_rect, Header_src_rect, GraphicsUnit.Pixel)
Dim src_rect As New Rectangle(_HScroll.Value, _HeaderHeight + 1 + _VScroll.Value, _Plan.Width, _Plan.Height)
Dim dst_rect As New Rectangle(0, _HeaderHeight + 1, _Plan.Width, _Plan.Height)
G.DrawImage(_Plan, dst_rect, src_rect, GraphicsUnit.Pixel)
_HScroll.LargeChange = G.ClipBounds.Width * 0.9
_VScroll.LargeChange = G.ClipBounds.Height * 0.9
End Sub
I would do all your drawing to an off-screen bitmap using the GDI graphics system. You can draw your headers and the rest of the chart as 2 distinct stages in the same bitmap. You would have to handle the scrolling yourself by watching the MouseMove event and checking the buttons status.
I am using GDI+ to draw the initial image on PictureBox - which renders a clean image. I am trying to then capture that drawing and draw it on a PDF using PDFSharp which works, but comes out blurry. I am sure it has something to do with the fact I have changed the destination Rectangle's size. What do i need to do to clean it up?
Code:
Dim bmp As New Bitmap(pb.Width, pb.Height)
Dim pdf As New PdfDocument
Dim page As PdfPage = pdf.AddPage
Dim g As XGraphics = XGraphics.FromPdfPage(page)
pb.DrawToBitmap(bmp, New Rectangle(pb.ClientRectangle.X, pb.ClientRectangle.Y, pb.Width, pb.Height + 20))
g.SmoothingMode = XSmoothingMode.AntiAlias
g.DrawImage(bmp, New XRect(New RectangleF(20, 0, 600, 800)))
pdf.Save(_path)
It's the anti-aliasing that makes images blurry.
AFAIK Adobe Reader draws images with anti-aliasing when used with PDFsharp.
With PDFsharp create an XImage from your BMP and then set
image.Interpolate = false;
for that XImage. This will give a hint to Adobe Reader that anti-aliasing is not wanted for that image.
With respect to screen shots, anti-aliasing is useful when scaling down (e.g. when taking a 400x300 bitmap from an 800x600 screen), but not when scaling up (like Adobe Reader will do with images embedded in PDF files).
See also:
http://forum.pdfsharp.net/viewtopic.php?p=5370#p5370
If you scale an image up, it will come out blurred. There is no other way since there are no additional information in the image and it will just be interpolated in some way. There is no "Zoom in and ENHANCE"-button. :-)
What I did in one of my programs where I wanted to save a controls current look to a file, was to first scale the control to the desired size, then draw it to the bitmap, then resize it down again.
If this works depends on the content of the control of course, wether it's scalable or not and so on.
e.g.
In the example below I have a Chart control that I work with in my export dialog called workingChart.
The steps that are used are:
Save old size
Resize chart to the desired size
Draw control to bitmap
Resize chart back to the old size
This works well and the image comes out crisp, since you do not resize the image itself.
Private Function GetChartScaledImage(wantsize As Size) As Bitmap
Dim oldsize As Size = workingChart.Size
Dim bmp As New Bitmap(wantsize.Width, wantsize.Height)
workingChart.Size = wantsize
workingChart.DrawToBitmap(bmp, New Rectangle(0, 0, wantsize.Width, wantsize.Height))
workingChart.Size = oldsize
Return bmp
End Function
I am attempting to overlay two transparent images within a winform, but it keeps rendering the form's background image behind the top transparent image, as opposed to the second image...
My basic set up is I have two panels and in each panel is a picturebox. Each image in the picture boxes have some transparent areas. I've set the BackColor of the panels to color.transparent.
When I make one panel overlay the other I'm seeing the form's backcolor come through as opposed to the underlaying image.
Am I missing a property that I can set?
You only need one picture box. The overlay can be done with graphics.
Imports System.Drawing
Dim OverlayImage As New Bitmap("Some Path", True)
Dim BackImage As New Bitmap("Some Path", True)
g As Graphics = Graphics.FromImage(BackImage)
g.DrawImage(OverlayImage, 0, 0)
pictureBox1.Image = BackImage
If you want to have the timer move the overlayed image, then first, make a variable Dim posX As Integer = 0
then use g.DrawImage(OverlayImage, posX, 0) Now when your timer ticks, increment posX by 10
Here's a complete function to overlay two images (adapted from Blue0500's answer):
''' <summary> Return a new image with one superimposed over the other. </summary>
Function OverlayImgs(ByVal BackgroundImg As System.Drawing.Bitmap, ByVal OverlayImg As System.Drawing.Bitmap, Position As System.Drawing.Point) As System.Drawing.Bitmap
Dim g = System.Drawing.Graphics.FromImage(BackgroundImg)
g.DrawImage(OverlayImg, Position)
Return BackgroundImg
End Function
Usage:
lblTest.Image = OverlayImgs(Img1, Img2, New Point(16, 16))
You don't need a PictureBox for this unless you are using it for the canvas. You can draw all images to a Rectangle structure and move them around. Personally I would create a class object that has a Rectangle, Image and other properties and hold them in a collection. Then you simply draw the class objects by there properties including location. If there images contain transparencies they will overlay for you. This also gives you a method to check for collisions via the Rectangle.IntersectsWith function.
I am building a 2D game where the user is a circle(:P) and the enemies are rectangles coming at him. However, my problem is that when I placed a very nice picture of space I found on the internet, the screen draws whatever it has to underneath this image. Everything works,I can still see my lives going down when something collides into me - except the fact it is all covered up by this picture.
In a nutshell, my question is: How Do I Draw everything ON Top of this - (I tried using the 'Send To Back' Command)
EDIT: The form draws everything through a timer, and the user controls his character through keys. This probably won't help - but if it does it's here.
Sorry folks didn't think you'd need the code. Here it is:
In the mybase.load procedure:
PicBackGround.Dock = DockStyle.Fill
PicBackGround is the picture box with the image.
In the paint procedure:
e.Graphics.Clear(Color.Black)
e.Graphics.FillEllipse(Brushes.Orange, Player)
'Projectiles
Dim i As Integer = 0
Do
If Current_Projectile(i).IsEmpty = False Then e.Graphics.FillRectangle(Brushes.Red, Current_Projectile(i))
i += 1
Loop Until i = UBound(Current_Projectile)
'Objects
i = 0
Do
If Objects(i).IsEmpty = False Then e.Graphics.FillRectangle(Brushes.Blue, Objects(i))
i += 1
Loop Until i = UBound(Objects)
Okay: Player is a rectangle declared right at the top, Dim Player As New Rectangle(0, 0, 50, 50);
There is then the array Objects, which stores all the data about the enemies coming at the player, Current_Projectiles is simply an array to store data about rectangles(bullets) that the player fires.
Anything else you want to know just let me know.
Yes, a control overlaps anything you draw on the form. A simple solution is to use the form's BackgroundImage property instead. Or draw the image yourself.
this is my first question on here.
I'm trying to build a dial control as a custom user control in VB.NET. I'm using VS2008.
so far I have managed to rotate image using graphics.rotatetransform . however, this rotate everything. Now I have a Bitmap for the dial which should stay stable and another Bitmap for the needle which I need to rotate.
so far i've tried this:
Dim gL As Graphics = Graphics.FromImage(bmpLongNeedle)
gL.TranslateTransform(bmpLongNeedle.Width / 2, bmpLongNeedle.Height * 0.74)
gL.RotateTransform(angleLongNeedle)
gL.TranslateTransform(-bmpLongNeedle.Width / 2, -bmpLongNeedle.Height * 0.74)
gL.DrawImage(bmpLongNeedle, 0, 0)
As I understand it, the image of the needle should be rotated at angle "angleLongNeedle" although i'm placing the rotated image at 0,0. However, the result is that the Needle doesn't get drawn on the control.
any pointers as to where I might be going wrong or something else I should be doing?
Thanks in advance
First of all, why do you allocate the Graphics object from a bitmap that you then proceed to draw onto the graphics? That doesn’t make sense.
Dim gL As Graphics = Graphics.FromImage(bmpLongNeedle)
' … '
gL.DrawImage(bmpLongNeedle, 0, 0)
What you probably want is a graphics context for the whole image. You then apply the transformations to it and finally draw the bmpLongNeedle image.
Secondly, your translations look inversed: in the first step, you need to move the image to the origin (0, 0); then you rotate it, and then move it back. So the transformation should look like this:
gL.TranslateTransform(-bmpLongNeedle.Width * 0.5, -bmpLongNeedle.Height * 0.5)
gL.RotateTransform(angleLongNeedle)
gL.TranslateTransform(bmpLongNeedle.Width * 0.5, bmpLongNeedle.Height * 0.5)
Notice the inversed order of the TranslateTransforms. Also, why did you translate by 0.74 times the height, instead of half?
oh the bitmap for needle has the pivot point at 0.74 * height.
may be I should have posted this before. but this is what i've done.
Public Class Altimeter
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim bmpBezel As New Bitmap("{path}\Altimeter_Background.bmp")
Dim bmpLongNeedle As New Bitmap("{path}\LongNeedle.bmp")
Dim rect2 As New Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height)
'make transparent
bmpBezel.MakeTransparent(Color.Yellow)
bmpLongNeedle.MakeTransparent(Color.Yellow)
Dim angleLongNeedle As Single = (Altitude / 50) * 360
'draw bezel
e.Graphics.DrawImage(bmpBezel, rect2)
'rotate long needle
Dim gL As Graphics = Graphics.FromImage(bmpLongNeedle)
gL.TranslateTransform(bmpLongNeedle.Width / 2, bmpLongNeedle.Height * 0.74)
gL.RotateTransform(angleLongNeedle)
gL.TranslateTransform(-bmpLongNeedle.Width / 2, -bmpLongNeedle.Height * 0.74)
gL.DrawImage(bmpLongNeedle, 0, 0)
MyBase.OnPaint(e)
End Sub
i use e.graphics.drawimage to paint the whole image. i don't really understand what you said about having graphics object for all the images and then drawing the needle? do you have any pseudo code?
thanks