How to Copy Local Storage Image to Clipboard WinRT - c#

I have an application, where user download images to the Local App Storage, it works like this because in order for the application to display content properly we dont want the user to have direct contact with the images and alter them.
Now we want the user to be able to copy the image to the clipboard or to make a copy in Pictures Library. The problem is that I'm getting this exception when trying to do it.
The code example is here:
http://imgur.com/qnltKTa
If I copy the image selecting it with the file picker, or getting it from Pictures Library my code works fine. But this happens when doing it from the Local Storage Folder.

Your code has mistake. Remove "\\" from this line
StorageFile storageFile = await folder.GetFileAsync("Circulo 2_Atomo.png")

Related

Is there a way in Blazor to prompt the user to browse to a local folder and select it for downloading a file?

The user needs to be able to upload a bunch of images which my app will merge into a single PDF. Then the user needs to be able to save that PDF to their local machine. My Blazor C# application uses <InputFile...> to prompt the user to select one or more images from their local machine. The app then creates the PDF file and adds the images to it. I need a way for the user to specify where they want to save the PDF. Or, failing that, I need a way to save it to a pre-determined folder (the application folder or whatever) and a fixed name (such as mergedImages.pdf) and then open the saved file in a browser window from which the user can download it to wherever they want.
I've tried adding a button with an href that points to the downloaded file, but the browser always blocks the link, returning the error: "Not allowed to load local resource: file:///D:/IVG_Blazor/MergeImagesIntoPDF/mergedImages.pdf".
Another way I thought of was to have it write the PDF to the downloads folder and display an icon at the bottom of the page that gives the user the options "Open", "Always open files of this type", "Show in folder", "Cancel". But I don't know how to implement that.
Does anyone know how to give the user easy access to such a file? Thanks.
As I understand, you are able to get the result of the merge operation, I assume it is a Stream or a byte array. And you want the user to be able to download it, and the user should be able to save it in a specific folder.
It seems that the second point is not possible, according to this post : Download A File At Different Location Using HTML5
For the first point, the user be able to download the merged PDF File, all to do is to have the Stream (or byte array), and to have a javascript function to do so.
C# :
private async Task DownloadFileFromStream(Stream fileStream, string fileName)
{
using var streamRef = new DotNetStreamReference(stream: fileStream);
await jsRuntime.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
}
Javascript :
async function downloadFileFromStream(fileName, contentStreamReference) {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer]);
const url = URL.createObjectURL(blob);
triggerFileDownload(fileName, url);
URL.revokeObjectURL(url);
}
function triggerFileDownload(fileName, url) {
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
}
It will trigger the download procedure of the browser.
Good luck :)
I came across your question when I was trying to solve my problem. I wanted to bring up a window to save a file in which the user could select a folder. Before that, the system automatically saved to the Downloads folder.
As I understood, there was no such possibility before, but now it is possible thanks to filesystem api: https://web.dev/file-system-access/#ask-the-user-to-pick-a-file-to-read.
I found this solution in the answer to the question referenced by Dylan: https://stackoverflow.com/a/70001920/16740180.
Unfortunately, this doesn't work for mobile devices right now, but it works fine for windows. I hope this helps someone.

How can I load image into picture box from another computer?

I need my picture box to load image from another computer. It means a different IP address. I have tried loading image from same computer and it works. But I need to load image from a separate server PC. The code below does not work.
pbFeature.Image = Image.FromFile(#"\192.168.232.100\C:\pic\a.png");
Network paths start with \\ not \. Assuming the file is shared correctly you just need pbFeature.Image = Image.FromFile(#"\\192.168.232.100\C:\pic\a.png");
Paste that same path into windows explorer and it should open the image if it is shared correctly.
share the file/folder to access by another computer. then you access file like below
var file= File.ReadAllText(#"\\server\path\filename.png");
then use the file where ever you want

C# UWP Copy database from installed location to local folder

I have a database on "ms-appx:///Assets/mydbfile.db" . I want to copy it to the local application folder when the application will start the first time. Database Contain a large number of data.
I try with this Code. but it gives me an exception.
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///Assets/mydbfile.db"));
await file.CopyAsync(ApplicationData.Current.LocalFolder, "mydbfile.db");
Here Is My Db File
I tried with a text file and your code worked fine.
Do you have the Build Action in the file properties set to Content?
Where are you trying to run your code within your app?

How to handle saved images locally vs on the server

I want to save some generated images.
The part which im confused about is how to handle the paths for saving files.
When running locally (in VS) the images can be saved in c:\images, but on the server id probably want to save them in root-of-webdir\images
do i need a configurable path in the web.config? how can i read the url path for the images in both cases?
You can use Server.MapPath to get the local directory name:
var folder = Server.MapPath("~/images");
This will usually give you 'c:\inetpub\wwwroot\application\images'.

Windows 8 App and access to file system

I'm at the beginning of my project and I wonder which technology I should use.
In my little research I found WinRT API being kind of pleasant and I really like tile grid concept in UI.
The only problem is that my app will generate tons of data - important data - which I have to store somewhere on the local machine. By 'somewhere' I mean use of a different partition than the OS.
So, why not to try this simple code.
await Windows.Storage.PathIO.WriteTextAsync(#"d:\tests\test.txt", "Hello World");
Because E_ACCESSDENIED, that's why. Windows 8 slaps me in face screaming "Access Denied".
Is there any way I can store my data in a way I like or Win8 is too h4x0r proof?
And no, "Make a desktop application" is not a correct answer.
All you need to know about file access and permissions in Windows Store Apps.
First of all, when storing config data you have two options:
Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
Which will use the roaming profile space so it will be stored in Cloud or Domain Profile
Windows.Storage.ApplicationDataContainer settings = Windows.Storage.ApplicationData.Current.LocalSettings;
Which will use the local profile space
Of course they both will be stored in the end under your users %appdata% but the roaming one will actually be synched if I understand everything correctly :)
So, for the application data that you would like to store on another partition:
First you need to select the location by using a FolderPicker
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
//Add some other yada yada to make the picker work as needed
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
Then you need to put the selected folder in an access list to remember that it's allowed to use this folder
StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
That way the application / system will keep track that it's allowed to use this folder in the future. The selected folder could be anywhere in the file system where you have access.
Finally if you wan't to get the selected folder back next time the app starts you simply do the opposite:
StorageFolder folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("TargetFolderToken",AccessCacheOptions.FastLocationsOnly);
The value FastLocationsOnly means that it will only return local drives. "TargetFolderToken" is the same identifier you used when you stored the folder in the FutureAccessList.

Categories

Resources