I am creating dynamic images on canvas . T want to translate them and rotate them on button click . Translating working fine but when I rotating it its gives error like this
Additional information:
Unable to cast object of type
'Windows.UI.Xaml.Media.TranslateTransform'
to type
'Windows.UI.Xaml.Media.RotateTransform'.
Here is my code
Image i = new Image(); // Global variable for selecting
private void btn_Click(object sender, RoutedEventArgs e) // For creating images
{
int i = 0;
Image image = new Image();
string url = "ms-appx:///Assets/1.png";
BitmapImage bm = new BitmapImage();
bm.UriSource = new Uri(url, UriKind.Absolute);
image.Source = bm;
image.Height = Double.NaN;
image.Width = Double.NaN;
image.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY | ManipulationModes.Rotate;
image.RenderTransform = new TranslateTransform();
image.Name = "img" + i;
image.Tapped += select;
image.ManipulationDelta += DragableItem_ManipulationDelta;
DrawCanvas.Children.Add(image);
i++;
}
private void select(object sender, TappedRoutedEventArgs e) // selecting of image
{
i = (Image)sender;
}
private void rotate_Click(object sender, RoutedEventArgs e) //rotating
{
if (i != null)
{
var translate = (RotateTransform)i.RenderTransform;
translate.CenterY = 0;
translate.CenterX = 0;
translate.Angle = 45;
i.RenderTransform = translate;
}
}
void DragableItem_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e) // translating code
{
var name = (Image)sender;
var translate = (TranslateTransform)name.RenderTransform;
translate.X += e.Delta.Translation.X;
translate.Y += e.Delta.Translation.Y;
}
need help stuck here for a long time ??
You are setting your RenderTransform to a TranslateTransform and then trying to cast it to a RotateTransform (as the error states). You should probably use a CompositeTransform instead or use a TransformGroup with both of the transform types in it.
Related
I have added a marker using GMap with the lat/long specified. When the application starts, the marker is placed in the incorrect position(at the center of the GMap control) and then when I zoom, it then goes to the specified coordinates. Is this a bug in GMap or am I doing something wrong? Here is the code.
GMapOverlay markersOverlay, mo2;
GMarkerGoogle marker, marker5;
GMapOverlay polyOverlay;
List<PointLatLng> points;
GMapRoute gr;
Graphics g;
bool start = true;
double move = .0001;
double lt = 73, lg = -180;
public Form1()
{
AllocConsole();
InitializeComponent();
try
{
System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("www.google.com");
}
catch
{
gmap.Manager.Mode = AccessMode.CacheOnly;
MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET - Demo.WindowsForms", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
gmap.MapProvider = GMapProviders.BingHybridMap;
gmap.Position = new PointLatLng(32, -100);
gmap.MinZoom = 3;
gmap.MaxZoom = 15;
gmap.Zoom = 9;
markersOverlay = new GMapOverlay("markers");
mo2 = new GMapOverlay("markers5");
marker5 = new GMarkerGoogle(new PointLatLng(lt, lg), GMarkerGoogleType.orange_small);
g = this.CreateGraphics();
}
private void Form1_Load(object sender, EventArgs e)
{
gmap.DragButton = MouseButtons.Left;
gmap.ShowCenter = false;
points = new List<PointLatLng>();
polyOverlay = new GMapOverlay("polygons");
GMapPolygon polygon = new GMapPolygon(points, "mypolygon");
polygon.Fill = new SolidBrush(Color.FromArgb(50, Color.Magenta));
polygon.Stroke = new Pen(Color.Magenta, 2);
}
protected void OnMouseMove(object sender, MouseEventArgs e)
{
PointLatLng p = gmap.FromLocalToLatLng(e.X, e.Y);
MouseLatLong.Text = Convert.ToString(p);
}
private void SubmitButton_Click(object sender, EventArgs e)
{
marker = new GMarkerGoogle(new PointLatLng(double.Parse(LattextBox.Text), double.Parse(LongtextBox.Text)), new Bitmap(#"C:\Users\Vaib\Documents\Visual Studio 2013\Projects\testGmap\testGmap\Resources\wpt.png"));
mo2.Markers.Add(marker);
gmap.Overlays.Add(mo2);
marker.ToolTip = new GMapToolTip(marker);
marker.ToolTipText = NametextBox.Text;
marker.ToolTipMode = MarkerTooltipMode.Always;
if (start)
{
gmap.Position = new PointLatLng(marker.Position.Lat, marker.Position.Lng);
start = false;
}
points.Add(new PointLatLng(marker.Position.Lat, marker.Position.Lng));
gr = new GMapRoute(points, "route");
gr.Stroke = new Pen(Color.Magenta, 2);
polyOverlay.Routes.Add(gr);
gmap.Overlays.Add(polyOverlay);
ga = new GMarkerArrow(new PointLatLng(gr.From.Value.Lat, gr.From.Value.Lng));
if (points.Count >= 2)
{
ga.Bearing = (float)final(gr.From.Value.Lat, gr.From.Value.Lng, points[1].Lat, points[1].Lng);
}
markersOverlay.Clear();
markersOverlay.Markers.Add(ga);
gmap.Overlays.Add(markersOverlay);
}
The trick is to first add overlay and then the marker:
gMapControl.Overlays.Add(markersOverlay);
markersOverlay.Markers.Add(marker);
Solution
Like you can read in the comments: Adding
gmap.Overlays.Clear()
at the very beginning of the method
private void SubmitButton_Click(object sender, EventArgs e)
was the answer to his problem.
I'm working in MSVC2010 (C++) on a WinForms app and had the same problem - took most of the afternoon to resolve.
This thread was useful, but I find all you need to do is (sorry it's not C#) is comment out the first time you add the marker - see
// DO NOT ADD... line
// Make marker
WindowsForms::Markers::GMarkerGoogle ^MyMarker;
WindowsForms::Markers::GMarkerGoogleType MyType = safe_cast<WindowsForms::Markers::GMarkerGoogleType>(3); // Blue marker 3
MyMarker = gcnew WindowsForms::Markers::GMarkerGoogle( PointLatLng(40.7, -74.0), MyType);
// MyOverlay->Markers->Add(MyMarker); // DO NOT ADD THE MARKER!!!
gMapControl1->Overlays->Add(MyOverlay);
MyMarker = gcnew WindowsForms::Markers::GMarkerGoogle( PointLatLng(40.7, -74.0), MyType);
MyOverlay->Markers->Add(MyMarker);
gMapControl1->Overlays->Add(MyOverlay);
gMapControl1->ReloadMap();
Basically i am trying to make a windows phone application where one by one images can be added on the canvas but the following code doesnt seem to work
private void AddImage_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask chooseImage = new PhotoChooserTask();
chooseImage.Completed += chooseImage_Completed;
chooseImage.Show();
}
public void chooseImage_Completed(object sender, PhotoResult e)
{
if (e.TaskResult != TaskResult.OK || e.ChosenPhoto == null)
{
return;
}
Image img = new Image();
SelectedBitmap = new WriteableBitmap(60,60);
img.Width = 100;
img.Height = 100;
img.Name = "img";
SelectedBitmap.SetSource(e.ChosenPhoto);
img.Source = SelectedBitmap;
e.ChosenPhoto.Position = 0;
CollageCanvas.Children.Add(img);
Canvas.SetTop(img,50);
Canvas.SetLeft(img,50);
}
the first time the button is clicked, image is successfully added to the canvas but when i attempt to add another image to the canvas it gives the following exception :-
"An exception of type 'System.ArgumentException' occurred in System.Windows.ni.dll but was not handled in user code
Additional information: Value does not fall within the expected range."
So i want to know how can i change my program to be able to add multiple images to the canvas.
I think it has something to do with the fact that the name property of each child element has to be unique. I think this should work for you.
private int index = 1;
public void chooseImage_Completed(object sender, PhotoResult e)
{
if (e.TaskResult != TaskResult.OK || e.ChosenPhoto == null)
{
return;
}
Image img = new Image();
WriteableBitmap SelectedBitmap = new WriteableBitmap(60, 60);
img.Width = 100;
img.Height = 100;
img.Name = "img";
SelectedBitmap.SetSource(e.ChosenPhoto);
img.Source = SelectedBitmap;
img.Name = "Photo " + index++; // Set unique name here
e.ChosenPhoto.Position = 0;
CollageCanvas.Children.Add(img);
Canvas.SetTop(img, 50);
Canvas.SetLeft(img, 50);
}
How can I manage that the event LongListSelector_SelectionChanged run first, then Image_MouseLeftButtonDown run after that
int count = 0;
Image LastImage = null, curImage = null;
BitmapImage bi1 = new BitmapImage();
BitmapImage bi2 = new BitmapImage();
int itemIndex;
void getImageLink()
{
for (int i = 0; i < CImageControl.lstImage.Count; i++)
s.Add(CImageControl.lstImage[i].ImageLink);
}
private void lstView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var myItem = ((LongListSelector)sender).SelectedItem as CImage;
itemIndex = ((LongListSelector)sender).ItemsSource.IndexOf(myItem);
//MessageBox.Show(myIndex.ToString());
}
then this will run
private void Image_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
string lastImageSource, lastImageAltSource, curImageSource, curImageAltSource;
count++;
if (count % 2 != 0)
{
LastImage = (Image)sender;
lastImageSource = s[itemIndex];
lastImageAltSource = lastImage.Source.ToString();
}
else
{
curImage = (Image)sender;
curImageSource = s[itemIndex];
curImageAltSource = curImage.Source.ToString();
}
}
ImageAltSouce is the display of the image, and I wanted to replace with ImageSource. But I need itemIndex to find index of images in Longlistselector before I can change any source. Since the event Image_MouseLeftButtonDown occur first, so I cant do anything
I have created a function that loops a folder and retrieves each image file and draw a picturebox on the form.
Here is the function :
private void Create_Controls(string Img_path)
{
PictureBox p = new PictureBox();
p.Size = new Size(138, 100);
p.Location = new Point(6, 6);
p.BackgroundImage = Image.FromFile(Img_path);
p.BackgroundImageLayout = ImageLayout.Stretch;
this.Controls.Add(p);
}
So what i need to do is : whenever i click on any picturebox on the form , a message popup with the image file path.
So i thought about adding a custom event :
p.Click += delegate { Pop_Up(); };
AND
private void Pop_Up()
{
/* POP UP MESSAGE WITH Picturebox image file path*/
}
You need to use the property ImageLocation of the PictureBox . The property gets or sets the path or URL for the image to display in the PictureBox.
Just do the following:
p.Click += new EventHandler(Pop_Up);
...
private void Pop_Up(object sender, EventArgs e)
{
var pb = sender as PictureBox;
if(pb != null)
MessageBox.Show(pb.ImageLocation);
}
You could use Tag property for this.
something like this .
private void Create_Controls(string Img_path)
{
PictureBox p = new PictureBox();
p.Size = new Size(138, 100);
p.Location = new Point(6, 6);
p.Tag = Img_path;
p.BackgroundImage = Image.FromFile(Img_path);
p.BackgroundImageLayout = ImageLayout.Stretch;
this.Controls.Add(p);
}
private void Pop_Up()
{
MessageBox.Show(p.Tag);
}
For more on this Go here.
Then along with what HatSoft says, change your Pop_up() method like:
private void Pop_Up(object sender, EventArgs e)
{
MessageBox.Show(((PictureBox)sender).ImageLocation);
}
But maybe a bit more elegant and checking if it is indeed a PictureBox etc.
I m trying to do wat my title above says however i m there is no change on my pic when i scroll the trackbar. i noe there is sth missing in my code ... can anyone help me out with this ? greatly appreciate it.
private void SetTrackBarProp()
{
trackBar1.Maximum = 255;
trackBar1.Minimum = 0;
trackBar1.TickFrequency = 1;
}
private Bitmap ApplyRGBFilter(Bitmap pic2)
{
ColorFiltering filter = new ColorFiltering();
filter.Red = new IntRange(0, red);
filter.Blue = new IntRange(0, blue);
filter.Green = new IntRange(0, green);
Bitmap processedImage = filter.Apply(pic2);
return processedImage;
}
void picturebox2(object sender, PaintEventArgs e)
{
pictureBox2.Image = ApplyRGBFilter(pic2);
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
red = trackBar1.Value;
}
Above is just a snippet of my code on the filtering part, "pic2" is the image that i can browsed from my computer with the browse button i had in my application. pictureBox2 will be the place where my pic2 will appear.
THANKS A LOT IN ADVANCE !
It is not clear that the header files for the ColorFiltering function. The only drawback of this Program is note of Header files.