C# UWP - How to get an object's drag image? - c#

How can I get a copy of the drag image for an item I'm dragging in my application?
The DragStarting event contains a DragStartingEventArgs object that includes a DragUI object whose description is, "The visual representation of the data being dragged." Ideally, I'd like to do something like this within the DragStarting event:
private void OnDragStarting( UIElement sender, DragStartingEventArgs args )
{
// Create a new bitmap image object
var dragImage = new BitmapImage();
// Assign the drag image to the new bitmap image object
dragImage = args.DragUI. ????
}
However, there doesn't seem to be a way to get the drag image from the DragUI object. DragUI contains only "Set*" methods, and no "Get*" methods.
Is there a way to get the drag image as the drag operation begins?

You can use DataView.GetStorageItemsAsync() to receive the items you dragged into your applications.
<Grid AllowDrop="True" DragOver="Image_drop_drag_over_ui" Drop="image_drop"/>
//C# code
public async void Image_Drop(object sender, DragEventArgs e)
{
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
List<StorageFile> received_images = new List<StorageFile>();
var items = await e.DataView.GetStorageItemsAsync();
var storageFile = items[0] as StorageFile;
received_images.Add(storageFile);
}
}
private void Image_drop_drag_over_ui(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Copy;
e.DragUIOverride.Caption = "Drop receipt";
e.DragUIOverride.IsCaptionVisible = true;
}

Related

Toggle the picture of picture box with picture box on_click c#

I have one image in my picture box using the resource what i want is to change the image when i click the icon.png it should be changed to icon1.png within the picturebox then when i click again the picture box it should be changed to icon.png
private void pictureBox10_Click(object sender, EventArgs e)
{
if (pictureBox10.ImageLocation != #"icon1.png")
{
var image = Image.FromFile(#"icon1.png");
pictureBox10.Image = image;
}
if (pictureBox10.ImageLocation == #"icon1.png")
{
var image = Image.FromFile(#"icon.png");
pictureBox10.Image = image;
}
}
but its not working please help me out of this.
You're getting a null from the image location as it's not set when you're assigning a picture to the Image property. There are a few ways to fix this:
Change the assignment so you assign using the ImageLocation
pictureBox10.ImageLocation = #"icon1.png";
Change the check to see if the Image property is equal to your new Image
pictureBox10.Image == Image.FromFile(#"icon.png");
Set the image location at the same time you set the image property
pictureBox10.Image == Image.FromFile(#"icon.png");
pictureBox10.ImageLocation = #"icon.png" ;
I feel the second might not come back as equal, you probably want to try the first one or third
Suggested code:
private void pictureBox10_Click(object sender, EventArgs e)
{
if (pictureBox10.ImageLocation != #"icon1.png")
{
pictureBox10.ImageLocation = #"icon1.png"
}
if (pictureBox10.ImageLocation == #"icon1.png")
{
pictureBox10.ImageLocation = #"icon.png";
}
}
Or:
private void pictureBox10_Click(object sender, EventArgs e)
{
if (pictureBox10.ImageLocation != #"icon1.png")
{
var image = Image.FromFile(#"icon1.png");
pictureBox10.Image = image;
pictureBox10.ImageLocation = #"icon1.png";
}
if (pictureBox10.ImageLocation == #"icon1.png")
{
var image = Image.FromFile(#"icon.png");
pictureBox10.Image = image;
pictureBox10.ImageLocation = #"icon.png";
}
}
You would also need to update your intial property setting to set the ImageLocation and not the Image property or to set the ImageLocation at the same time you set the Image file
EDIT
Off the top of my head, to set the property initially, you can do this (Source):
protected override void OnLoad(EventArgs e){
pictureBox10.ImageLocation = #"icon.png";
}
Though I can't remember if the PictureBox would have been created then, if not then use the onShown event instead (Source)
EDIT 2
Here is another way to create the event and set the property, first follow the steps here to add the event onShown to the form. You need to click on the form itself and not the controls inside the form to find the event.
Once done, inside the event add the following code:
pictureBox10.ImageLocation = #"icon.png";
That should help to resolve your issue
Try to directly reference the picture into the picture box:
pictureBox10.Image = Image.FromFile(#"Images\a.bmp");
Source
Thanks every one thanks a lot but there was some problem but have solved this issue so m writing the actual code...
public Form1()
{
InitializeComponent();
pictureBox10.ImageLocation = #"icon.png";
}
private void pictureBox10_Click(object sender, EventArgs e)
{
if (pictureBox10.ImageLocation == #"icon1.png")
{
pictureBox10.ImageLocation = #"icon.png";
}
else
{
pictureBox10.ImageLocation = #"icon1.png";
}
}
first you have to initialize the image location then use two if condition i think that was the main problem use if else anyway thaks every one thaks a lot special thaks to #Draken

Image disappears when dropped outside our pictureboxes

First off, thanks for reading this and taking the time to try help us.
We are currently making this project for our c# class, we had to study Drag&Drop by ourselves but we are having a problem.
Situation:
We have 6 starting pictureboxes with images, we also have 6 textboxes with a description, the idea is simple, match them together. However, if we drag an image from the starting picturebox into one of the 6 answer pictureboxes but by accident drop it on the form, it disappears.
We are clueless to find a way to actually 'reset' the image back to starting position when not placed in a picturebox.
private void Picture_MouseDown(object sender, MouseEventArgs e)
{
try
{
PictureBox source = (PictureBox)sender;
naam = source.ImageLocation;
source.DoDragDrop(source.Image, DragDropEffects.Move);
source.Image = null;
}
catch (NullReferenceException)
{
try
{
throw new RijException("Picturebox is momenteel leeg.");
}
catch (RijException)
{
}
}
}
private void Picture_DragEnter(object sender, DragEventArgs e)
{
PictureBox source = (PictureBox)sender;
e.Effect = DragDropEffects.Move;
}
private void Picture_DragDrop(object sender, DragEventArgs e)
{
PictureBox source = (PictureBox)sender;
source.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
source.Tag = naam;
}
So when we actually drop the image (while dragging) outside the picturebox (in the form) It will just disappear, What we need is a solution to reset it to starting position, but don't know how we could manage to pull that off.

WPF dealing with files in a listBox

I intend to create a .mp4 Listbox from which I can play an .mp4 of my choice.
I've already created the .mp4 player(by drag&drop) and I'm dealing with trouble with how to deal with the listBox.
private void button1_Click(object sender, RoutedEventArgs e)
{
var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
try
{
DirectoryInfo dr = new DirectoryInfo(dialog.SelectedPath.ToString());
if (result == FORMS.DialogResult.OK)
{
foreach (FileInfo f in dr.GetFiles())
{
listBox1.Items.Add(f);
}
}
}
catch { }
}
This only enables me to get all the .mp4 files(from selected folder) to be shown in the listBox,
How do I manage to drag objects from the listBox into the .mp4 player(which is already drag and drop enabled).
You can do it using the DragDrop.DoDragDrop method.
Example:
<ListBox SelectionChanged="Selector_OnSelectionChanged">
<ListBoxItem>X</ListBoxItem>
<ListBoxItem>Y</ListBoxItem>
<ListBoxItem>Z</ListBoxItem>
</ListBox>
And in code
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listbox = sender as ListBox;
DragDrop.DoDragDrop(listbox, listbox.SelectedItem, DragDropEffects.None);
}
Could be that in your mp4 player you'll need to implement more than just enable drag&Drop (like extract dragged item and play it ...)
You can find more information about on MSDN Drag and Drop Overview page.
Hope this helps
:) Thanks alot, that really helps! You were right about the .mp4 player, he does need to identify the drag onto him but I just couldn't figure out how. I did manage to write the code to play an .mp4 by dragging an .mp4 directly from your computer (desktop for example) and here's the code for that:
private void Grid_Drop(object sender, DragEventArgs e)
{
string filename = (string)((DataObject)e.Data).GetFileDropList()[0];
mediaElement1.Source =new Uri(filename);
mediaElement1.LoadedBehavior = MediaState.Manual;
mediaElement1.UnloadedBehavior = MediaState.Manual;
mediaElement1.Volume = (double)slider_vol.Value;
mediaElement1.Play();
}
There are other add-ons to that code but they don't really matter.
What function from Drag and Drop should I use on the "Play" button for the player? and how do I get the filename to actually handle that file? Thanks!

Drag and Drop one control to another control in winform

am doing something very very simple.
I have a listbox whose events are set like this :
public Form1()
{
InitializeComponent();
this.listBox1.AllowDrop = true;
this.listBox1.DragEnter += new DragEventHandler(listBox1_DragEnter);
this.listBox1.DragDrop += new DragEventHandler(listBox1_DragDrop);
}
void listBox1_DragDrop(object sender, DragEventArgs e)
{
//code to add labelText to Items of ListBox
}
void listBox1_DragEnter(object sender, DragEventArgs e)
{
//set DragDropEffects;
}
now I have a label, code for which is as follows:
private void label1_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop((sender as Label).Text, DragDropEffects.Copy);
//this.label1.DoDragDrop((sender as Label).Text, DragDropEffects.Copy);
//used one of them at a time.
}
but nothing happens. listbox DragEnter event never fires up. in fact, drag never happens.
whenever i try to drag label (text), not allowed windows cursor appears, instead of 'DragDropEffects.Copy's cursor
Drag and Drop doesn't take place..
when I modify the listbox (and the associated code) to accept files to be dropped on it from any other window, that works perfectly.
so..am unable to perform drag from a control kept on the form to another control kept on the same form.
am I missing something? am running windows XP.
I went through this and through this
please help...
Your code does work actually.
You just have to set the right drag effects in your event handlers.
void listBox1_DragDrop(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
void listBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
Check if ListBox.AllowDrop is set to TRUE or not
The following is an example of what you need, with all the code (adding it here for whoever finds this post).
#region Initial Values
//Constructor:
public Form1() {
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e ) {
InitialValues();
}
private void InitialValues() {
PrepareDragAndDrop();
}
#endregion Initial Values
#region Drag & Drop
private void PrepareDragAndDrop() {
//For the object that receives the other dragged element:
TheSamplListBox.AllowDrop = true;
TheSamplListBox.DragEnter += TheSamplListBox_DragEnter;
TheSamplListBox.DragLeave += TheSamplListBox_DragLeave;
TheSamplListBox.DragDrop += TheSamplListBox_DragDrop;
//For the object that will be dragged:
TheSampleLabel.MouseDown += ( sender, args ) => DoDragDrop( TheSampleLabel.Text, DragDropEffects.Copy );
}
private void TheSamplListBox_DragEnter( object theReceiver, DragEventArgs theEventData ) {
theEventData.Effect = DragDropEffects.Copy;
//Only the code above is strictly for the Drag & Drop. The following is for user feedback:
//You can use [TheSamplListBox] but this approach allows for multiple receivers of the same type:
var theReceiverListBox = (ListBox) theReceiver;
theReceiverListBox.BackColor = Color.LightSteelBlue;
}
private void TheSamplListBox_DragLeave( object theReceiver, EventArgs theEventData ) {
//No code here for the Drag & Drop. The following is for user feedback:
//You can use [TheSamplListBox] but this approach allows for multiple receivers of the same type:
var theReceiverListBox = (ListBox) theReceiver;
theReceiverListBox.BackColor = Color.White;
}
private void TheSamplListBox_DragDrop( object theReceiver, DragEventArgs theEventData ) {
//You can use [TheSamplListBox] but this approach allows for multiple receivers of the same type:
var theReceiverListBox = (ListBox) theReceiver;
//Get the data being dropped. In this case, a string:
var theStringBeingDropped = theEventData.Data.GetData( "System.String" );
//Add the string to the ListBox:
theReceiverListBox.Items.Add( theStringBeingDropped );
//Only the code above is strictly for the Drag & Drop. The following is for user feedback:
theReceiverListBox.BackColor = Color.White;
}
#endregion Drag & Drop
.

How can I make a scrollabe image list in C# Windows Form?

I need to create thumbnails list of pictures located on web.
I would like also to add CheckBox to make thumbnails choose able.
I'm trying to load pictures from urls to ListBox
// from form design file:
System.Windows.Forms.ListBox listBoxPictures;
// from main file
foreach (Photo albumPhoto in album.Photos)
{
PictureBox albumsImg = new PictureBox();
albumsImg.LoadAsync(albumPhoto.URL); // URL is string
CheckBox selectedPhotoCheckBox = new CheckBox();
listBoxPictures.Items.Add(albumsImg);
listBoxPictures.Items.Add(selectedPhotoCheckBox);
}
It doesn't work, there are no images appear in ListBox.
What am I doing wrong?
How can I make a scrollabe image list in C# Windows Form?
what is wrong is that you have to wait load completed of picture
private void button1_Click(object sender, EventArgs e)
{
//make your loop here
pictureBox1.WaitOnLoad = false;
pictureBox1.LoadCompleted += new AsyncCompletedEventHandler(pictureBox1_LoadCompleted);
pictureBox1.LoadAsync(albumPhoto.URL);
}
void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
//now your picture is loaded, add to list view
CheckBox selectedPhotoCheckBox = new CheckBox();
listBoxPictures.Items.Add(albumsImg);
listBoxPictures.Items.Add(selectedPhotoCheckBox);
}

Categories