I started a windows form application which imports images from files and store them and specify a radio button to each one.
it have a button with the name 'Lock'
so if user select one radio button and then press the button the app should change the lock screen image and then Lock the screen.
But I don't know how to set the image and lock the screen.
I googled later and the answer about the LockScreen.blabla didn't work for me because I can't do using windows.system.userprofile;
If some one get me the assembly i will do the thing.
there is the events:
private void rB_CheckedChanged(object sender, EventArgs e)
{
MyRadioButton radioButton = sender as MyRadioButton;
pictureBox1.Image = radioButton.Image;
}
private void btnBrowse_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
foreach (Control control in FindForm().Controls)
{
if (control.GetType() == typeof(MyRadioButton))
{
MyRadioButton mrb = control as MyRadioButton;
if (mrb.Checked == true)
{
mrb.Image = pictureBox1.Image;
}
}
}
}
}
private void btnLock_Click(object sender, EventArgs e)
{
//should set the lock screen background
LockScreen.LockScreen.SetImageFileAsync(imagefile);//shows error 'the name lock screen does not exist
}
I have made a UWP application to use the LockScreen class provided by windows.userProfile.dll And the event's changed to:
private Dictionary<string, string> Images = new Dictionary<string, string>();
private async void btnLock_Click(object sender, RoutedEventArgs e)
{
string path;
if (Images.TryGetValue(selectedRadioButton.Name, out path))
{
StorageFile file = await StorageFile.GetFileFromPathAsync(path);
await LockScreen.SetImageFileAsync(file);
try
{
HttpClient httpClient = new HttpClient();
Uri uri = new Uri("http://localhost:8080/lock/");
HttpStringContent content = new HttpStringContent(
"{ \"pass\": \"theMorteza#1378App\" }",
UnicodeEncoding.Utf8,
"application/json");
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(
uri,
content);
httpResponseMessage.EnsureSuccessStatusCode();
var httpResponseBody = await httpResponseMessage.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
throw ex;
}
}
}
private async void btnBrowse_Click(object sender, RoutedEventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
using (var imageStream = await file.OpenReadAsync())
{
var bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(imageStream);
image.Source = bitmapImage;
}
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
await file.CopyAsync(storageFolder, selectedRadioButton.Name+file.FileType,NameCollisionOption.ReplaceExisting);
addOrUpdate(selectedRadioButton.Name, Path.Combine(storageFolder.Path, selectedRadioButton.Name + file.FileType));
save();
}
}
private async void rb_Checked(object sender, RoutedEventArgs e)
{
RadioButton rb = sender as RadioButton;
selectedRadioButton = rb;
btnBrowse.IsEnabled = true;
string path;
if (Images.TryGetValue(rb.Name, out path))
{
StorageFile file = await StorageFile.GetFileFromPathAsync(path);
using (var imageStream = await file.OpenReadAsync())
{
var bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(imageStream);
image.Source = bitmapImage;
}
}
}
private void addOrUpdate(string key, string image)
{
if (Images.Keys.Contains(key))
{
Images.Remove(key);
Images.Add(key, image);
}
else
{
Images.Add(key, image);
}
}
private async void save()
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile file = await storageFolder.CreateFileAsync("sample.txt", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, JsonConvert.SerializeObject(Images));
}
private async void load()
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.GetFileAsync("sample.txt");
string fileText = await FileIO.ReadTextAsync(sampleFile);
try
{
Images = JsonConvert.DeserializeObject<Dictionary<string, string>>(fileText);
}
catch (Exception)
{
}
}
and you can see in the lock button click event there is a request to a web server cause you cannot directly lock the screen in a UWP app and you can follow the rest of your question in my next answer to the question "why I can't directly lock screen in UWP app?and how to do so?".
Related
I have a CommandBar in my Windows Store app, and when I click the Open button on my CommandBar, it runs the OpenFile handler as follows:
private async void OpenFile(object sender, RoutedEventArgs e)
{
MessageDialog dialog = new MessageDialog("You are about to open a new file. Do you want to save your work first?");
dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(SaveAndOpen)));
dialog.Commands.Add(new UICommand("No", new UICommandInvokedHandler(Open)));
await dialog.ShowAsync();
}
private async void SaveAndOpen(IUICommand command)
{
await SaveFile();
Open(command);
}
private async void Open(IUICommand command)
{
FileOpenPicker fileOpenPicker = new FileOpenPicker();
fileOpenPicker.ViewMode = PickerViewMode.List;
fileOpenPicker.FileTypeFilter.Add(".txt");
StorageFile file = await fileOpenPicker.PickSingleFileAsync();
await LoadFile(file);
}
I see the message just fine, but only when I hit Yes do I get presented with the FileOpenPicker. When I hit No I get an UnauthorizedAccessException: Access is denied. at the following line: StorageFile file = await fileOpenPicker.PickSingleFileAsync();
I'm baffled...does anyone know why this is happening? I even tried running it in a dispatcher on the off-chance that the handler was being called on a different thread, but...unfortunately, same thing:
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, async () =>
{
StorageFile file = await fileOpenPicker.PickSingleFileAsync();
await LoadFile(file);
});
Yes, it was due to RT's dialog race conditions. The solution was for me to literally make use of the MessageDialog class the same way you would use MessageBox.Show in WinForms:
private async void OpenFile(object sender, RoutedEventArgs e)
{
MessageDialog dialog = new MessageDialog("You are about to open a new file. Do you want to save your work first?");
IUICommand result = null;
dialog.Commands.Add(new UICommand("Yes", (x) =>
{
result = x;
}));
dialog.Commands.Add(new UICommand("No", (x) =>
{
result = x;
}));
await dialog.ShowAsync();
if (result.Label == "Yes")
{
await SaveFile();
}
FileOpenPicker fileOpenPicker = new FileOpenPicker();
fileOpenPicker.ViewMode = PickerViewMode.List;
fileOpenPicker.FileTypeFilter.Add(".txt");
StorageFile file = await fileOpenPicker.PickSingleFileAsync();
await LoadFile(file);
}
I want to open FileOpenPicker for two times one after the other.i.e)After selecting the image from FileOpenPicker for first time it returns back to the page with the selected image,that time a MessageDialog is shown,And clicking OK button on MessageDialog I am opening the FileOpenPicker again,this time I get a weird crash on
open.FileTypeFilter.Clear();
as 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'.And My code is below.
private async void PickImage()
{
FileOpenPicker open = new FileOpenPicker();
open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
open.ViewMode = PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types
open.FileTypeFilter.Clear();
open.FileTypeFilter.Add(".bmp");
open.FileTypeFilter.Add(".png");
open.FileTypeFilter.Add(".jpeg");
open.FileTypeFilter.Add(".jpg");
StorageFile file = await open.PickSingleFileAsync();
if (file != null)
{
if (prop1.Source == null)
{
Dialogpopup();
return;
}
else if (prop2.Source == null)
{
Dialogpopup();
return;
}
}
}
private async void Dialogpopup()
{
MessageDialog msgDialog = new MessageDialog("Would you like to add additional photos?");
UICommand okBtn = new UICommand("Yes");
okBtn.Invoked = OkBtnClick;
msgDialog.Commands.Add(okBtn);
UICommand cancelBtn = new UICommand("No");
cancelBtn.Invoked = CancelBtnClick;
msgDialog.Commands.Add(cancelBtn);
//Show message
msgDialog.ShowAsync();
}
private async void OkBtnClick(IUICommand command)
{
img_PointerPressed(img_galary, null);
}
private void img_PointerPressed(object sender, PointerRoutedEventArgs e)
{
if (sender == img_galary)
{
PickImage();
}
}
I'm trying to implement some logical to share the image of my webView and some extras informations. If I do that without capture screen, it works perfectly:
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
if (await GetShareContent(e.Request))
{
if (String.IsNullOrEmpty(e.Request.Data.Properties.Title))
{
e.Request.FailWithDisplayText("Nenhum título adicionado");
}
}
}
private async Task<bool> GetShareContent(DataRequest request, StorageFile file)
{
bool succeeded = false;
string text = "Dados do Arquivo:" + Environment.NewLine + webViewModel.Name;
string dataPackageText = text;
if (!String.IsNullOrEmpty(dataPackageText))
{
DataPackage requestData = request.Data;
requestData.Properties.Title = "Target";
requestData.Properties.Description = webViewModel.Name;
requestData.SetText(dataPackageText);
succeeded = true;
}
else
{
request.FailWithDisplayText("Não há nada para compartilhar");
}
return succeeded;
}
But, if I try the same thing justing adding the captured image, it doesn't work, doesn't show any Excepetion, just the message: "Não há nada para compartilhar agora" (There's nothing to share right now)
I don't know what is going on. The code that doesn't work:
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
StorageFile file = await captureScreen();
if (await GetShareContent(e.Request, file))
{
if (String.IsNullOrEmpty(e.Request.Data.Properties.Title))
{
e.Request.FailWithDisplayText("Nenhum título adicionado");
}
}
}
private async Task<StorageFile> captureScreen()
{
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(webView, (int)webView.Width, (int)webView.Height);
Image myImage = new Image();
myImage.Source = renderTargetBitmap;
var file = await App.rootDir.CreateFileAsync("screenCapture.jpg", CreationCollisionOption.ReplaceExisting);
var pixels = await renderTargetBitmap.GetPixelsAsync();
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
byte[] bytes = pixels.ToArray();
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)webView.Width, (uint)webView.Height,
96, 96, bytes);
await encoder.FlushAsync();
}
return file;
}
I though it could be happening because the image was not read when the share is called, but I'm using await as it should be. And my jpeg is created perfectly.
The OnDataRequested callback needs to take a deferral, using DataRequest.GetDeferral, when calling asynchronous APIs.
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
DataRequestDeferral deferral = e.Request.GetDeferral();
// Code to do screen capture...
deferral.Complete();
}
But, per MSDN, "[the share operation] function must return a DataPackage object within 200ms to prevent the operation from timing out". It is definitely possible for the screen capture to take longer than 200 ms. Use the DataPackage.SetDataProvider for operations that may take longer such as screen capture.
private void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
DataPackage requestData = e.request.Data;
requestData.Properties.Title = "Target";
requestData.Properties.Description = webViewModel.Name;
// Set up the data provider for a long running share operation...
requestData.SetDataProvider(StandardDataFormats.Bitmap, OnDeferredRequestedHandler);
}
private async void OnDeferredRequestedHandler(DataProviderRequest providerRequest)
{
// Again, get a deferral as an asynchronous method is called
DataProviderDeferral deferral = providerRequest.GetDeferral();
// Code to do screen capture...
deferral.Complete();
}
The Share content source sample on MSDN shows how this can be performed in full detail.
i have been working on a windows store project using c#
i have a method called
void TranscodeProgress(IAsyncActionWithProgress<double> asyncInfo, double percent)
{
pg1.Value=percent;
}
when i try to add a progress bar to this it gives me an error
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
please help me to correct this error
thanks
this is my entire code
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
Windows.Storage.StorageFile source;
Windows.Storage.StorageFile destination;
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary;
openPicker.FileTypeFilter.Add(".mp4");
openPicker.FileTypeFilter.Add(".wmv");
source = await openPicker.PickSingleFileAsync();
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.VideosLibrary;
savePicker.DefaultFileExtension = ".wmv";
savePicker.SuggestedFileName = "New Video";
savePicker.FileTypeChoices.Add("MPEG4", new string[] { ".wmv" });
destination = await savePicker.PickSaveFileAsync();
// Method to perform the transcoding.
TranscodeFile(source, destination);
}
async void TranscodeFile(StorageFile srcFile, StorageFile destFile)
{
MediaEncodingProfile profile =
MediaEncodingProfile.CreateWmv(VideoEncodingQuality.HD1080p);
MediaTranscoder transcoder = new MediaTranscoder();
PrepareTranscodeResult prepareOp = await
transcoder.PrepareFileTranscodeAsync(srcFile, destFile, profile);
if (prepareOp.CanTranscode)
{
var transcodeOp = prepareOp.TranscodeAsync();
transcodeOp.Progress +=
new AsyncActionProgressHandler<double>(TranscodeProgress);
// p1.Value = double.Parse(transcodeOp.Progress.ToString());
// txtProgress.Text = transcodeOp.Progress.ToString();
transcodeOp.Completed +=
new AsyncActionWithProgressCompletedHandler<double>(TranscodeComplete);
}
else
{
switch (prepareOp.FailureReason)
{
case TranscodeFailureReason.CodecNotFound:
MessageDialog md=new MessageDialog("Codec not found.");
await md.ShowAsync();
break;
case TranscodeFailureReason.InvalidProfile:
MessageDialog md1 = new MessageDialog("Invalid profile.");
await md1.ShowAsync();
break;
default:
MessageDialog md2 = new MessageDialog("Unknown failure.");
await md2.ShowAsync();
break;
}
}
//txtDisplay.Text = a;
}
void TranscodeProgress(IAsyncActionWithProgress<double> asyncInfo, double percent)
{
}
void TranscodeComplete(IAsyncActionWithProgress<double> asyncInfo, AsyncStatus status)
{
asyncInfo.GetResults();
if (asyncInfo.Status == AsyncStatus.Completed)
{
// Display or handle complete info.
}
else if (asyncInfo.Status == AsyncStatus.Canceled)
{
// Display or handle cancel info.
}
else
{
// Display or handle error info.
}
}
You should:
Avoid async void.
Use the TAP naming pattern (make your Task-returning methods end in "Async").
Use AsTask to do complex interop between TAP and WinRT asynchronous operations.
Something like this:
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
...
await TranscodeFileAsync(source, destination);
}
async Task TranscodeFileAsync(StorageFile srcFile, StorageFile destFile)
{
MediaEncodingProfile profile =
MediaEncodingProfile.CreateWmv(VideoEncodingQuality.HD1080p);
MediaTranscoder transcoder = new MediaTranscoder();
PrepareTranscodeResult prepareOp = await
transcoder.PrepareFileTranscodeAsync(srcFile, destFile, profile);
if (prepareOp.CanTranscode)
{
var progress = new Progress<double>(percent => { pg1.Value = percent; });
var result = await prepareOp.TranscodeAsync().AsTask(progress);
// Display result.
}
else
{
...
}
}
You are trying to access UI component from non UI Thread.
use:
void TranscodeProgress(IAsyncActionWithProgress<double> asyncInfo, double percent)
{
if(InvokeRequired)
{
Invoke(new MethodInvoker() => TranscodeProgress(asyncInfo, percent));
return;
}
pg1.Value=percent;
}
You cannot access UI components from non UI threads, Calling Invoke with a delegate passes the function call to thread that owns the component and than that thread call the passed delegate.
well i try a lot of things but i don't really understand everything of save in localstorage. I know how its work since the camera, but i don't know how to make it with inkManager. If you're any ideas ?
This is my code to save where the user wants, but i would like to "auto-save" in localstorage :
private async void save_Button(object sender, RoutedEventArgs e)
{
if (_inkManager.GetStrokes().Count > 0)
{
try
{
Windows.Storage.Pickers.FileSavePicker save = new Windows.Storage.Pickers.FileSavePicker();
save.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
save.DefaultFileExtension = ".jpg";
save.FileTypeChoices.Add("JPG", new string[] { ".jpg" });
StorageFile filesave = await save.PickSaveFileAsync();
IOutputStream ab = await filesave.OpenAsync(FileAccessMode.ReadWrite);
if (ab != null)
await _inkManager.SaveAsync(ab);
// await save.CopyAsync(ApplicationData.Current.LocalFolder, "merge1.jpg");
if (save != null)
{
Clipboard.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appdata:///local/merge1.jpg"));
}
}
catch (Exception)
{
}
}
}
The solution is pretty easy finally :
StorageFile myMerge = await ApplicationData.Current.LocalFolder.CreateFileAsync("myimg.png");
IOutputStream ac = await myMerge.OpenAsync(FileAccessMode.ReadWrite);
if (ac != null)
await _inkManager.SaveAsync(ac);