如何使用 C# 通过 Windows 8 应用中的移动服务上传图像,以及如何从存储下载下载图像



我正在开发一个应用程序,我想通过一个按钮将图像上传到 Windows 8 应用程序中的 Azure 存储,然后从另一个按钮我想在我的项目中的某个地方检索图像,但我没有得到任何适当的工作代码,任何人都可以帮助我解决这个问题。

为了上传图像,我正在使用以下代码:

     private async void Button_Click(object sender, RoutedEventArgs e)
    {
        // Objects/variables for the account, client uploading 
        // capability and the blob container 
        CloudStorageAccount account;
        CloudBlobClient blobClient;
        CloudBlobContainer container;

        //You could use local development storage 
        //   account = CloudStorageAccount.DevelopmentStorageAccount; 
        account = new CloudStorageAccount(
            new StorageCredentials(" name",
                " Key "), true);
        // blobClient is used to upload photos 
        blobClient = account.CreateCloudBlobClient();
        try
        {
            // The container name in Windows Azure Storage is "mypictures" 
            container = blobClient.GetContainerReference("mypictures");
            await container.CreateIfNotExistsAsync();
            // Make the photos publicly visible 
            BlobContainerPermissions permissions = new BlobContainerPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
            await container.SetPermissionsAsync(permissions);
            // Get a reference to the local machine's Pictures folder 
            StorageFolder storageFolder = KnownFolders.PicturesLibrary;
            // Get all files in the pictures folder 
         IReadOnlyList<StorageFile> storageFiles = await storageFolder.GetFilesAsync();

                          CloudBlockBlob blob = null;
            // Loop through pictures 
            foreach (StorageFile StorageFile in storageFiles)
            {
                using (IRandomAccessStream imageStream = await StorageFile.OpenReadAsync())
                {
                    // Name the file in the cloud the same as on local files sytem 
                    blob = container.GetBlockBlobReference(StorageFile.Name);
                    // Upload file to Windows Azure Storage 
                    await blob.UploadFromStreamAsync(imageStream);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        } 
    }

但是当我单击按钮上传图像时,它根本不起作用,这意味着它无法打开图片库。

private void image_upload_Click(object sender, RoutedEventArgs e)
{
    CameraCaptureTask camera = new CameraCaptureTask();
    camera.Show();
    camera.Completed += camera_Completed;
}
private void camera_Completed(object sender, PhotoResult e)
{
    txtProgress.Text = string.Empty;
    if (e.TaskResult == TaskResult.OK)
    {
        BitmapImage bitmap = new BitmapImage();
        bitmap.SetSource(e.ChosenPhoto);
        WriteableBitmap wb = new WriteableBitmap(bitmap);
        using (MemoryStream stream = new MemoryStream())
        {
            wb.SaveJpeg(stream, wb.PixelWidth, wb.PixelHeight, 0, 85);
            byte[] imageBytes = stream.ToArray();
            AzureStorage storage = new AzureStorage();
            storage.Account = "azure storage";
            storage.BlobEndPoint = "azure end point";
            storage.Key = "you azure key";
            string fileName = "CapturedImage-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
            bool error = false;
            if (!error)
            {
                Dispatcher.BeginInvoke(() => txtProgress.Text += "Uploading image into Cloud...");
                storage.PutBlob("test", fileName, imageBytes, error);
                if (!error)
                {
                    Dispatcher.BeginInvoke(() => txtProgress.Text += "Updating informations...");
                }
                else
                {
                    MessageBox.Show("Error uploading the new Avatar.nCode: [PUT]n");
                }
            }
            else
            {
                MessageBox.Show("Error uploading the new Avatar.nCode: [DEL]n");
            }

最新更新