Hope you can help. Stuck on a simple problem for some, I'm a noob. What in trying to do is get an image objects in Silverlight/C# to drop randomly from the top of the canvas, at the moment its going right to left.
This is the from the object class.
namespace LOLWordGame
{
public class LetterA : ContentControl, IGameEntity
{
private int speed = 0;
public LetterA()
{
Image LetterImage = new Image();
LetterImage.Height = 45;
LetterImage.Width = 45;
LetterImage.Source = new BitmapImage(new Uri("images/a.png", UriKind.RelativeOrAbsolute));
this.Content = LetterImage;
Random random = new Random();
Canvas.SetLeft(this, -20);
Canvas.SetTop(this, random.Next(250, 850)); //randomly
speed = random.Next(1, 5);
}
public void Update(Canvas c)
{
Move(Direction.Down);
if (Canvas.GetLeft(this) < 100)
{
c.Children.Remove(this);
}
}
public void Move(Direction direction)
{
Canvas.SetLeft(this, Canvas.GetLeft(this) - speed);
}
}
}
Thanks in advance.
For a solution: maybe you should use the Canvase.SetTop Method instead of the SetLeft method? Hope this helps.
Secondary.. I'm sure following code is not the solution to your problem but I refactored it a bit. Try using collection initializers. You have a method Move that you only call once and the method has only one line of code: no reason to make that a method in my opinion. Also the method takes in a parameter but you do not use it inside the method.
public class LetterA : ContentControl, IGameEntity
{
private int speed = 0;
public LetterA()
{
var letterImage = new Image()
{
Height = 45,
Width = 45,
Source = new BitmapImage(new Uri("images/a.png", UriKind.RelativeOrAbsolute))
};
Content = letterImage;
var random = new Random();
Canvas.SetLeft(this, -20);
Canvas.SetTop(this, random.Next(250, 850));
speed = random.Next(1, 5);
}
public void Update(Canvas c)
{
Canvas.SetLeft(this, Canvas.GetLeft(this) - speed);
if (Canvas.GetLeft(this) < 100)
c.Children.Remove(this);
}
}
Related
Here's what I used to solve this problem:
public class Snake {
List<Rectangle> bodyParts = new List<Rectangle>();
}
Snake snk = new Snake();
snk.bodyParts.Add(new Rectangle(760,25,8,8))
//the Exception Occurs here
//snk.bodyParts[0].Y = snk.bodyPArts[0].Y-10;
//Here's my approach
snk.bodyParts[0] = new Rectangle(bodyParts[0].X,bodyParts[0].Y-10,8,8);
Exception: Cannot modify the return value of 'System.Collections.Generic.List.this[int]'
My question is: Are they any alternative/better ways to manage this exception ?
Thanks.
EDIT: I got my answer, can you please close this question.
Rather than working directly with Rectangles, how about adding a BodyPart class, with some manipulation methods:
public class BodyPart
{
private Rectangle rectangle;
public BodyPart(Rectangle rectangle)
{
this.rectangle = rectangle;
}
public void MoveY(int value)
{
rectangle.Y += value;
}
public void MoveX(int value)
{
rectangle.X += value;
}
}
public class Snake
{
public List<BodyPart> Parts = new List<BodyPart>();
}
public class AppSnake
{
public void Run()
{
var snake = new Snake();
snake.Parts.Add(new BodyPart(new Rectangle(760, 25, 8, 8)));
snake.Parts[0].MoveY(-10);
}
}
Rectangle, RectangleF, Point, PointF, Size and SizeF are value types (struct type). This means that you can not/should not change an individual part of the structure.
The reason is that unlike classes each variable keeps its own copy of the structure. When you type list[0].X = 10, the indexer list[0] returns a copy of the rectangle and not the original value. The correct way is to assign a new rectangle list[0] = A which copies all the values from A into the array.
Please read more on value types and structs before attempting to write code that uses them.
The quickest way to fix your code without completely changing it around is by adding methods that manipulate all the body parts in predefined ways:
public class Snake
{
public List<Rectangle> Parts { get; } = new List<Rectangle>();
public void MoveX(int delta)
{
for(int i = 0; i < Parts.Count; i++)
{
// Read the current rectangle from the list
var rect = Parts[i];
// Change the coordinate.
rect.X += delta;
// Write the modified rectangle back into the list
Parts[i] = rect;
}
}
public void MoveY(int delta)
{
for(int i = 0; i < Parts.Count; i++)
{
// Read the current rectangle from the list
var rect = Parts[i];
// Change the coordinate.
rect.Y += delta;
// Write the modified rectangle back into the list
Parts[i] = rect;
}
}
}
Use Rectangle.Location and Rectanagle.Size properties e.g.:
snk.bodyParts[0].Location = new Point(newX, newY);
snk.bodyParts[0].Size = new Size(newWidth, newHeight);
I can change my markers position with
markers.Markers[2].Position = new PointLatLng(30.0000, 30.00000);
but how can i change marker icon with setting a varible like above?
I am declaring points as
GMap.NET.WindowsForms.GMapMarker marker3 =
new GMap.NET.WindowsForms.Markers.GMarkerGoogle(
new GMap.NET.PointLatLng(30.0000, 30.00000),
new Bitmap("images/2.png"));
thanks...
To solve this problem, i contacted the creator of the library: radioman.
He referred some code called 'GMarkerArrow.' Here is the code:
namespace Demo.WindowsForms.CustomMarkers
{
using System;
using System.Drawing;
using System.Runtime.Serialization;
using GMap.NET;
using GMap.NET.WindowsForms;
[Serializable]
public class GMarkerArrow : GMapMarker, ISerializable
{
[NonSerialized]
public Brush Fill = new SolidBrush(Color.FromArgb(155, Color.Blue));
//[NonSerialized]
//public Pen Pen = new Pen(Brushes.Blue, 5);
static readonly Point[] Arrow = new Point[] { new Point(-7, 7), new Point(0, -22), new Point(7, 7), new Point(0, 2) };
public float Bearing = 0;
private float scale = 1;
public float Scale
{
get
{
return scale;
}
set
{
scale = value;
Size = new System.Drawing.Size((int)(14 * scale), (int)(14 * scale));
Offset = new System.Drawing.Point(-Size.Width / 2, (int)(-Size.Height / 1.4));
}
}
public GMarkerArrow(PointLatLng p)
: base(p)
{
Scale = (float)1.4;
}
public override void OnRender(Graphics g)
{
//g.DrawRectangle(Pen, new System.Drawing.Rectangle(LocalPosition.X, LocalPosition.Y, Size.Width, Size.Height));
{
g.TranslateTransform(ToolTipPosition.X, ToolTipPosition.Y);
var c = g.BeginContainer();
{
g.RotateTransform(Bearing - Overlay.Control.Bearing);
g.ScaleTransform(Scale, Scale);
g.FillPolygon(Fill, Arrow);
}
g.EndContainer(c);
g.TranslateTransform(-ToolTipPosition.X, -ToolTipPosition.Y);
}
}
public override void Dispose()
{
//if(Pen != null)
//{
// Pen.Dispose();
// Pen = null;
//}
if (Fill != null)
{
Fill.Dispose();
Fill = null;
}
base.Dispose();
}
#region ISerializable Members
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
protected GMarkerArrow(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endregion
}
}
Save this as GMarkerArrow.cs and add it to your project.
Add this to your Form1 code:
using Demo.WindowsForms.CustomMarkers;
Now you can use the new marker via following:
GMarkerArrow marker1 = new GMarkerArrow(new PointLatLng(-30, -40));
marker1.ToolTipText = "blablabla";
marker1.ToolTip.Fill = Brushes.Black;
marker1.ToolTip.Foreground = Brushes.White;
marker1.ToolTip.Stroke = Pens.Black;
marker1.Bearing = 30; // Rotation angle
marker1.Fill = new SolidBrush(Color.FromArgb(155, Color.Blue)); // Arrow color
markers.Markers.Add(marker1);
gMapControl1.Overlays.Add(markers);
Also i want to thanks #rdoubleui , thanks you sir.
I hope this helps to everyone.
GMapOverlay markersOverlay;
private void AddOrUpdateMarker(string tag, double lat, double lng, Image NewImage)
{
Bitmap bmpmarker = (Bitmap)NewImage;
var marker = markersOverlay.Markers.FirstOrDefault(m => m.Tag == tag);
if(marker!=null)
{
markersOverlay.Markers.Remove(marker);
gMapControl1.Refresh();
marker = new GMarkerGoogle(new PointLatLng(lat, lng), bmpmarker);
marker.Tag = tag;
markersOverlay.Markers.Add(marker);
gMapControl1.Refresh();
}
else
{
marker = new GMarkerGoogle(new PointLatLng(lat, lng), bmpmarker);
marker.Tag = tag;
markersOverlay.Markers.Add(marker);
gMapControl1.Refresh();
}
}
The problem updating the image of that marker is that the Image property is not publicly accessible, therefore you can not update the image that way.
There are two possibilities: first one is to replace the marker reference with a new one giving you the opportunity to set a new image and copying the position of the current marker. However that is not the clean way as you unnecessarily create a whole bunch of references only to dispose them right away depending on the use case. If it's a one-time update, then this approach is fine.
The preferable way is to derive from GMapMarker as the Google marker does (you can use that as a template, leaving out the whole google specific icon logic). Are you familiar with the concept of deriving? It will require some more effort but will be worth it, could help with that.
Also your main reference is probably the project's github page.
EDIT
using System.Drawing;
public class GImageMarker : GMapMarker
{
public Bitmap Bitmap { get; set; }
public GImageMarker(PointLatLng p, Bitmap Bitmap)
: base(p)
{
this.Bitmap = Bitmap;
Size = new System.Drawing.Size(Bitmap.Width, Bitmap.Height);
Offset = new Point(-Size.Width / 2, -Size.Height);
}
public override void OnRender(Graphics g)
{
g.DrawImage(Bitmap, LocalPosition.X, LocalPosition.Y, Size.Width, Size.Height);
}
}
I have class, that creates Shapes for me (I tried to create some kind of "class factory" but im not sure if this is correct term for that I have created.
Problem is described in comments in my code.
public static Ellipse SomeCircle()
{
Ellipse e = new Ellipse();
double size = 10;
e.Height = size;
e.Width = size;
e.Fill = new SolidColorBrush(Colors.Orange);
e.Fill.Opacity = 0.8;
e.Stroke = new SolidColorBrush(Colors.Black);
// i want to have something like this here:
// canvas1.Children.Add(e);
// but I cant access non-static canvas1 from here
// I need this to place my ellipse in desired place
// (line below will not work if my Ellipse is not placed on canvas
// e.Margin = new Thickness(p.X - e.Width * 2, p.Y - e.Height * 2, 0, 0);
return e;
}
I have no idea how to workaround this.
I don't want to pass that canvas by parameter in my whole application...
Since you do not want to pass your Canvas around as a parameter, you could try creating an Extension Method which would act on your Canvas Object.
namespace CustomExtensions
{
public static class Shapes
{
public static Ellipse SomeCircle(this Canvas dest)
{
Ellipse e = new Ellipse();
double size = 10;
e.Height = size;
e.Width = size;
e.Fill = new SolidColorBrush(Colors.Orange);
e.Fill.Opacity = 0.8;
e.Stroke = new SolidColorBrush(Colors.Black);
dest.Children.Add(e);
return e;
}
}
}
Usage remember to add the CustomExtensions Namespace to your usings.
canvas1.SomeCircle();
I am attempting to render some Image objects in a canvas (Layout) on the main form of my application which is called Main. I am using C# and WPF to create the application but am unable to get the images to render from within another class but works without problems in the Main form partial class.
public static void renderStarscape(int density = 200)
{
Main main = new Main();
Random random = new Random();
for (int x = 0; x < density; x++)
{
int starSize = random.Next(1, 10);
int starOpacity = random.Next(10, 30);
int starX = random.Next(0, 800);
int starY = random.Next(0, 500);
Image Star = new Image();
Star.Name = (x < 10) ? "star_0" + x : "star_" + x;
Star.Source = streamImage("star_background.png");
Star.Height = starSize;
Star.Width = starSize;
Star.Opacity = (double)starOpacity / 100;
main.Layout.Children.Add(Star);
Canvas.SetLeft(Star, starX);
Canvas.SetTop(Star, starY);
Canvas.SetZIndex(Star, 0);
}
}
Any help would be great, thanks
You are creating an all new instance of Main in the method which is not same as the one shown on UI.
Instead pass on the instance of the Canvas to the method and draw on that something like this -
public static void renderStarscape( Canvas layout, int density = 200)
{
// Use layout and remove the Main object initialization from the method.
layout.Children.Add(Star);
}
The reason its working in partial declaration of class is that you are using the same instance and not creating the new one for Main().
I need to rotate an image that is passed to a function as an array of BitmapFrames. the finished product needs to be saved as a BitmapFrame as well so I can send it to my Export-Image function. Help?
[Cmdlet(VerbsData.ConvertTo, "Rotate")]
public class RotateCmdlet : PSCmdlet
{
private BitmapFrame[] bFrame, outFrame;
private BitmapSource src;
private double pixelsize;
private int degrees;
private byte[] pixels, outPixels;
[Parameter(ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true), ValidateNotNullOrEmpty]
public BitmapFrame[] Bitmap
{
get
{
return bFrame;
}
set
{
bFrame = value;
}
}
[Parameter(Position = 0), ValidateNotNullOrEmpty]
public int Degrees
{
get
{
return degrees;
}
set
{
degrees = value;
}
}
protected override void ProcessRecord()
{
base.ProcessRecord();
Console.Write("Rotating the image {0} degrees...\n\n", degrees);
outFrame = new BitmapFrame[bFrame.Length];
for (int c = 0; c < bFrame.Length; c++)
{
Image image;
pixelsize = bFrame[c].PixelWidth * bFrame[c].PixelHeight;
pixels = new byte[(int)pixelsize];
outPixels = new byte[(int)pixelsize];
bFrame[c].CopyPixels(pixels, (int)(bFrame[c].Width * (bFrame[c].Format.BitsPerPixel / 8.0)), 0);
Stream strm = new MemoryStream(pixels);
image = Image.FromStream(strm);
var newBitmap = new Bitmap((int)bFrame[c].PixelWidth, (int)bFrame[c].PixelHeight);
var graphics = Graphics.FromImage(newBitmap);
graphics.TranslateTransform((float)bFrame[c].PixelWidth / 2, (float)bFrame[c].PixelHeight / 2);
graphics.RotateTransform(degrees);
graphics.TranslateTransform(-(float)bFrame[c].PixelWidth / 2, -(float)bFrame[c].PixelHeight / 2);
graphics.DrawImage(image, new System.Drawing.Point(0, 0));
for (int i = 0; i < pixelsize; i++)
{
outPixels[i] = pixels[i];
}
src = BitmapSource.Create(bFrame[c].PixelWidth, bFrame[c].PixelHeight, bFrame[c].DpiX, bFrame[c].DpiY,
bFrame[c].Format, bFrame[c].Palette, outPixels, (int)(bFrame[c].Width * (bFrame[c].Format.BitsPerPixel / 8)));
outFrame[c] = BitmapFrame.Create(src);
}
WriteObject(outFrame);
}
}
It seems that the error you're getting on the FromStream call might be because the image format is invalid. This could be for many reasons and since I'm not sure exactly how this Cmdlet is being used I will make an assumption:
I may be incorrect here, but since you're passing in an array of BitMapFrame's, I'm wondering if the ProcessRecord is being called once for each array element. The only way to truly tell is to see how you're Cmdlet is being invoked. For example, if your BitMap parameter is coming from the pipeline then there is a good chance that ProcessRecord is being called once for each BitMapFrame in the array.
Does that make sense?