This must be a very noobie question so please don't judge me too hard!
In Windows Form Applications something like this would easily get the bound of a progressbar:
progressBar1.Bounds.X(or)Y
How can I get the same in WPF? I can't find anything on this on internet.
This is my progressbar OnClick method for the windows form app:
private void progressBar1_MouseClick(object sender, MouseEventArgs e)
{
float absoluteMouse = (PointToClient(MousePosition).X - progressBar1.Bounds.X);
float calcFactor = progressBar1.Width / (float)100;
float relativeMouse = absoluteMouse / calcFactor;
double maxlength = BASS_ChannelBytes2Seconds(BassHandle, (ulong)PlayBackLength);
double percents = (maxlength * relativeMouse) / 100;
UInt64 pos = BASS_ChannelSeconds2Bytes(BassHandle, percents);
BASS_ChannelSetPosition(BassHandle, pos, 0);
progressBar1.Value = Convert.ToInt32(relativeMouse);
int current = BASS_ChannelGetPosition(BassHandle, 0);
TimeLabel.Text = FormatTime(BASS_ChannelBytes2Seconds(BassHandle, (uint)current)) + "/" + FormatTime(BASS_ChannelBytes2Seconds(BassHandle, (uint)PlayBackLength));
}
This is my attempt on it's WPF version
private void progressBar1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
double absoluteMouse = (PointFromScreen(GetMousePosition()).X - progressBar1.?);
double calcFactor = progressBar1.Width / (float)100;
double relativeMouse = absoluteMouse / calcFactor;
double maxlength = Imports.BASS_ChannelBytes2Seconds(GlobalVariables.BassHandle, (ulong)GlobalVariables.PlayBackLength);
double percents = (maxlength * relativeMouse) / 100;
UInt64 pos = Imports.BASS_ChannelSeconds2Bytes(GlobalVariables.BassHandle, percents);
Imports.BASS_ChannelSetPosition(GlobalVariables.BassHandle, pos, 0);
progressBar1.Value = Convert.ToInt32(relativeMouse);
int current = Imports.BASS_ChannelGetPosition(GlobalVariables.BassHandle, 0);
TimeLabel.Text = FormatTime(Imports.BASS_ChannelBytes2Seconds(GlobalVariables.BassHandle, (uint)current)) + "/" + FormatTime(Imports.BASS_ChannelBytes2Seconds(GlobalVariables.BassHandle, (uint)GlobalVariables.PlayBackLength));
}
You may use progressBar1.Margin.Left - for X, progressBar1.Margin.Top - for Y.
But there will be value relative to Container where ProgressBar is placed.
Related
How can I enable zoom on a contentpage in xamarin forms? Is it possible to enable it on the entire contentpage? Or is it only possible to zoom in on images?
You can use the pinch gesture inside a ContentPage, here is the official page:
https://developer.xamarin.com/guides/xamarin-forms/user-interface/gestures/pinch/
and for the entire project sample:
https://github.com/xamarin/xamarin-forms-samples/tree/master/WorkingWithGestures/PinchGesture
Here is an example of what you can achieve:
Xamarin.Forms Pinch Example
Try this Class , this solution do not scroll when you zoom .
Source found here:
Source Code
public class PinchToZoomContainer : ContentView {
double currentScale = 1;
double startScale = 1;
double xOffset = 0;
double yOffset = 0;
public PinchToZoomContainer ()
{
var pinchGesture = new PinchGestureRecognizer ();
pinchGesture.PinchUpdated += OnPinchUpdated;
GestureRecognizers.Add (pinchGesture);
}
void OnPinchUpdated (object sender, PinchGestureUpdatedEventArgs e)
{
if (e.Status == GestureStatus.Started) {
// Store the current scale factor applied to the wrapped user interface element,
// and zero the components for the center point of the translate transform.
startScale = Content.Scale;
Content.AnchorX = 0;
Content.AnchorY = 0;
}
if (e.Status == GestureStatus.Running) {
// Calculate the scale factor to be applied.
currentScale += (e.Scale - 1) * startScale;
currentScale = Math.Max (1, currentScale);
// The ScaleOrigin is in relative coordinates to the wrapped user interface element,
// so get the X pixel coordinate.
double renderedX = Content.X + xOffset;
double deltaX = renderedX / Width;
double deltaWidth = Width / (Content.Width * startScale);
double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;
// The ScaleOrigin is in relative coordinates to the wrapped user interface element,
// so get the Y pixel coordinate.
double renderedY = Content.Y + yOffset;
double deltaY = renderedY / Height;
double deltaHeight = Height / (Content.Height * startScale);
double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;
// Calculate the transformed element pixel coordinates.
double targetX = xOffset - (originX * Content.Width) * (currentScale - startScale);
double targetY = yOffset - (originY * Content.Height) * (currentScale - startScale);
// Apply translation based on the change in origin.
Content.TranslationX = targetX.Clamp (-Content.Width * (currentScale - 1), 0);
Content.TranslationY = targetY.Clamp (-Content.Height * (currentScale - 1), 0);
// Apply scale factor
Content.Scale = currentScale;
}
if (e.Status == GestureStatus.Completed) {
// Store the translation delta's of the wrapped user interface element.
xOffset = Content.TranslationX;
yOffset = Content.TranslationY;
}
}
}
Helper DoubleExtensions
public static class DoubleExtensions
{
public static double Clamp (this double self, double min, double max)
{
return Math.Min (max, Math.Max (self, min));
}
}
You can try using the Scale Api on the Content page. This seemed to work for me on a small test app.
public App ()
{
// The root page of your application
var scaleUp = new Button {
Text = "Scale Up"
};
scaleUp.Clicked += (sender, e) => {
this.MainPage.Scale += 1;
};
var scaleDown = new Button {
Text = "Scale Down"
};
scaleDown.Clicked += (object sender, EventArgs e) => {
this.MainPage.Scale -= 1;
};
MainPage = new ContentPage {
Content = new StackLayout {
VerticalOptions = LayoutOptions.Center,
Children = {
scaleUp,
scaleDown
}
}
};
}
Trying to use this in conjunction with other controls that take the Pan/scroll gesture seems to fail. ScrollView and ListView for example grab the Pinch gesture even though they themselves don't Pinch but they do scroll/pan. So it seems like Pinch inherits/uses that gesture so it is grabbed by the wrapped control. This seems to make Pinch a very niche feature that can only work on very static views of Label and Image for example, but nothing more complex.
You can use gestures.
Sample here : http://arteksoftware.com/gesture-recognizers-with-xamarin-forms/
I wrote a program which displays the color and the depth stream of a kinect sensor. In the depth image it's possible for the user to select two points and the program displays the coordinates of the points. Now I want to improve my program. So I added a new window called Disatance.xaml. This window is opened when the user checks a Check-box. In this window I want to show the coordinates, rather then in the MainWindow. So I need to pass the integer variables of the x- and y-Coordinates. My problem now is that I'm really new to C# and I don't know a way to pass integers from one window to an other window. I really how somebody can help me.
The main code is executed in the main window. In that I open the second window, by clicking on a button. My code looks like this:
private void CmdDistance_Click(object senderr, RoutedEventArgs e)
{
Distance distance = new Distance();
distance.Show();
}
The coordinates are currently calculatet and displayed by my function void Mouse_ClickTest:
private void Mouse_ClickTest(object sender, System.Windows.Input.MouseEventArgs e)
{
if (ChkDistance.IsChecked == true)
{
System.Windows.Point myMousePosition = e.GetPosition(DepthImage);
double xpixel = myMousePosition.X;
double ypixel = myMousePosition.Y;
int xpos = (int)xpixel;
int ypos = (int)ypixel;
if ((xpos > 0 || ypos > 0) && (xpos <= 640 || ypos <= 480))
{
int depthWidth = mySensor.DepthStream.FrameWidth;
int depthIndex = xpos + (ypos * depthWidth);
if (depthIndex < 307840)
{
SkeletonPoint mySkeletonArray = this.mySkeletonArray[depthIndex];
float zpos = mySkeletonArray.Z;
zpos = zpos * 1000;
int z = (int)zpos;
float xp = mySkeletonArray.X;
xp = xp * 1000;
int x = (int)xp;
float yp = mySkeletonArray.Y;
yp = yp * 1000;
int y = (int)yp;
if (e.LeftButton == MouseButtonState.Pressed)
{
if (i == false)
{
TxtCoordX.Text = string.Format("X: {0}", x);
TxtCoordY.Text = string.Format("Y: {0}", y);
TxtCoordZ.Text = string.Format("Z: {0}", z);
i = true;
}
else
{
TxtCoordX2.Text = string.Format("X: {0}", x);
TxtCoordY2.Text = string.Format("Y: {0}", y);
TxtCoordZ2.Text = string.Format("Z: {0}", z);
i = true;
}
}
}
else
{
return;
}
}
else
{
return;
}
}
}
Now I want to pass the integer variable x, y and z to the window Distance
Declare your Distance class higher in scope in your MainWindow class. The way you have it right now, it's created and goes out of scope once the Click handler completes execution.
The quickest and dirtiest way to do what you're asking is to create a method in the code-behind of your Distance form. It could be something like:
public void SetCoordinates(float x, float y, float z)
{
//Set properties here
}
Then you can make TextBlocks or TextBoxes to those properties.
You can also initialize the x,y,z to a value by passing them in the constructor:
Instead of:
new Distance();
Do:
new Distance(float x, float y, float z);
Then you can make TextBlocks or TextBoxes to those properties.
I have a BufferedGraphics painted in the panel. The image is zoomed in and zoomed out. From this buffered graphic images how can i find the mouse position in the image not in the panel.
private void panel2_paint(object sender, PaintEventArgs e)
{
if (bitmap != null)
{
float widthZoomed = panel2.Width / Zoom;
float heigthZoomed = panel2.Height / Zoom;
if (widthZoomed > 30000.0f)
{
Zoom = panel2.Width / 30000.0f;
widthZoomed = 30000.0f;
}
if (heigthZoomed > 30000.0f)
{
Zoom = panel2.Height / 30000.0f;
heigthZoomed = 30000.0f;
}
if (widthZoomed < 2.0f)
{
Zoom = panel2.Width / 2.0f;
widthZoomed = 2.0f;
}
if (heigthZoomed < 2.0f)
{
Zoom = panel2.Height / 2.0f;
heigthZoomed = 2.0f;
}
float wz2 = widthZoomed / 2.0f;
float hz2 = heigthZoomed / 2.0f;
Rectangle drawRect = new Rectangle(
(int)(viewPortCenter.X - wz2),
(int)(viewPortCenter.Y - hz2),
(int)(widthZoomed),
(int)(heigthZoomed));
drawrecX = drawRect.X;
drawrecY = drawRect.Y;
dispwidth = (int)(widthZoomed);
dispheight = (int)(heigthZoomed);
myBuffer.Graphics.Clear(Color.White); //Clear the Back buffer
Console.WriteLine(this.panel2.DisplayRectangle.Width);
Console.WriteLine(this.panel2.DisplayRectangle.Height);
myBuffer.Graphics.DrawImage(bitmap, this.panel2.DisplayRectangle, drawRect, GraphicsUnit.Pixel);
//pictureBox1.Image =
myBuffer.Render(this.panel2.CreateGraphics());
//this.toolStripStatusLabel1.Text = "Zoom: " + ((int)(this.Zoom * 100)).ToString() + "%";
}
}
the above code is the paint part.
To find the location of the mouse in the image i tried like this
private void panel2_DoubleClick(object sender, EventArgs e)
{
var mouseArgs = (MouseEventArgs)e;
double Pic_width = dispwidth / panel2.Width;//to find the relative position
double Pic_height = dispheight / panel2.Height;
int xpoint = (int)Pic_width * mouseArgs.X + drawrecX;//drawrecX is the X coordinate from the drawing image
int ypoint = (int)Pic_height * mouseArgs.Y + drawrecY;
}
but this code does not give me the exact position. any idea?
Try this:
private void panel2_DoubleClick(object sender, EventArgs e)
{
var mouseArgs = (MouseEventArgs)e;
int x = mouseArgs.X - drawrecX;
int y = mouseArgs.Y - drawrecY;
var size = ImageSizeWithoutZoom;
var zoomSize = ImageSizeWithZoom;
double xPoint = x * size.X / zoomSize.X;
double yPoint = y * size.Y / zoomSize.Y;
}
I am working with the XBox-Kinect (Beta SDK).
I try to direct a robot to grab an object from a box that contains many objects (bachelorthesis), for that I need to find the closest point of the nearest object to the kinect. Than i can localize the complete object by region growing and find out the grabbing point for the robot.
Now I have two question for that topic:
How can I find out the x and y coordinate of this point? ( I have the distance already)
How can I access the private byte[] FindMinimum(ImageFrame imageFrame)
by button click to find the closest distance?
Here is my code:
void nui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)
{
image1.Source = e.ImageFrame.ToBitmapSource();
byte[] Minumum = FindMinimum(e.ImageFrame);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
FindMinimum();
}
private byte[] FindMinimum(ImageFrame imageFrame)
{
int height = imageFrame.Image.Height;
int width = imageFrame.Image.Width;
Byte[] depthData = imageFrame.Image.Bits;
Byte[] colorFrame = new byte[imageFrame.Image.Height * imageFrame.Image.Width * 4];
int min = int.MaxValue, max = int.MinValue;
for (int i = 0; i < depthData.Length; i += 2)
{
int dist = GetDistance(depthData[i], depthData[i + 1]);
if (dist < min && dist > 0) min = dist;
if (dist > max) max = dist;
}
textBox1.Text = Convert.ToString(min);
return colorFrame;
}
private int GetDistance(byte firstFrame, byte secondFrame)
{
int distance = (int)(firstFrame | secondFrame << 8);
return distance;
}
private void Window_Closed(object sender, EventArgs e)
{
nui.Uninitialize();
}
}
}
I've looked all over for something to help me with this, but so far nothing. I am trying to create a program that allows a user to print a collection of pdfs. I am using ABCPDF9 to get my pdfs (most of which are stored as html) and append them all to a single ABCPDF.Doc object. The problem I'm getting is when I have these multiple pages I end up with only one page of the pdf printing. Here are some code snippets below.
private void ProcessAndPrintSelected()
{
var selectedForm = SubSonicRepository.Instance.CommunicationRepository.GetMessageTemplateByID((int)cmboChooseForm.SelectedValue);
_currentItemIndex = 0;
int itemsCount = dataGridViewLoans.RowCount;
_currentPrintPageIndex = 1;
foreach (DataGridViewRow row in this.dataGridViewLoans.Rows)
{
lblPrinterProgress.Text = "Printing document " + _currentItemIndex + " of " + itemsCount + ".";
lblPrinterProgress.Refresh();
Application.DoEvents();
BulkPrinterLoanModel loan = row.DataBoundItem as BulkPrinterLoanModel;
try
{
if (selectedForm.MailMessageContent != null)
{
byte[] formBytes = GetFormBytes(selectedForm.ID, loan.ApplicantID, loan.LoanID);
doc.Read(formBytes);
appendedDocs.Append(doc);
}
else
{
throw new InvalidOperationException("No PDF data to print.");
}
}
catch (Exception x)
{
//for now, don't do anything, not even logging, but don't halt queue either.
MessageBox.Show(x.ToString());
}
}
printDoc.PrintPage += new PrintPageEventHandler(pd_PrintPage);
printDoc.PrinterSettings.FromPage = 1;
printDoc.PrinterSettings.ToPage = appendedDocs.PageCount;
printDoc.PrinterSettings.MinimumPage = 1;
printDoc.PrinterSettings.MaximumPage = appendedDocs.PageCount;
PrintDialog pDialog = new PrintDialog();
pDialog.Document = printDoc;
pDialog.AllowSomePages = true;
if (pDialog.ShowDialog() == DialogResult.OK)
{
pDialog.Document.Print();
}
}
and my printpage event.
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
XRect cropBox = appendedDocs.CropBox;
double srcWidth = (cropBox.Width / 72) * 100;
double srcHeight = (cropBox.Height / 72) * 100;
double pageWidth = e.PageBounds.Width;
double pageHeight = e.PageBounds.Height;
double marginX = e.PageSettings.HardMarginX;
double marginY = e.PageSettings.HardMarginY;
//center it
double x = (pageWidth - srcWidth) / 2;
double y = (pageHeight - srcHeight) / 2;
x -= marginX;
y -= marginY;
RectangleF rect = new RectangleF((float)x, (float)y, (float)srcWidth, (float)srcHeight);
appendedDocs.Rect.SetRect(cropBox);
int rez = e.PageSettings.PrinterResolution.X;
appendedDocs.Rendering.DotsPerInch = rez;
Graphics g = e.Graphics;
using (Bitmap bitmap = appendedDocs.Rendering.GetBitmap())
{
g.DrawImage(bitmap, rect);
}
}
I've looked in the ABCPDF manual, but all the help on printing is presented in their sample project which I'm having a hard time understanding. Any help on this matter would be appreciated. Thanks :)
I got it, mostly from looking at the following question. I needed to use the Doc.PageNumber to access each page of the pdf. Here's the print page event where I changed the code.
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
_currentItemIndex++;//added index to keep track of page. default to 1
appendedDocs.PageNumber = _currentItemIndex;//set to current page for printing
XRect cropBox = appendedDocs.CropBox;
double srcWidth = (cropBox.Width / 72) * 100;
double srcHeight = (cropBox.Height / 72) * 100;
double pageWidth = e.PageBounds.Width;
double pageHeight = e.PageBounds.Height;
double marginX = e.PageSettings.HardMarginX;
double marginY = e.PageSettings.HardMarginY;
//center it
double x = (pageWidth - srcWidth) / 2;
double y = (pageHeight - srcHeight) / 2;
x -= marginX;
y -= marginY;
RectangleF rect = new RectangleF((float)x, (float)y, (float)srcWidth, (float)srcHeight);
appendedDocs.Rect.SetRect(cropBox);
int rez = e.PageSettings.PrinterResolution.X;
appendedDocs.Rendering.DotsPerInch = rez;
Graphics g = e.Graphics;
using (Bitmap bitmap = appendedDocs.Rendering.GetBitmap())
{
g.DrawImage(bitmap, rect);
}
e.HasMorePages = _currentItemIndex < appendedDocs.PageCount;//check for more pages.
}
Feel silly having asked this question and then answering myself. But it feels good knowing that this question is now out there for anyone else that gets stuck on the issue.