2016-02-23 76 views
0

我在使用Magick.NET时遇到了一些问题。 我想将我从URL下载的图像转换为内存流,然后我试图将图像转换为png。 我的代码执行时没有任何错误,并且创建(并上传)了一个文件,但由于某种原因该文件为0kb,并且无法读取。Magick.Net将ai转换成png

我的类看起来是这样的:

/// <summary> 
/// Used to help with image control 
/// </summary> 
public class ImageProvider 
{ 

    // Private property for the azure container 
    private readonly CloudBlobContainer container; 

    /// <summary> 
    /// Default constructor 
    /// </summary> 
    public ImageProvider() 
    { 

     // Get our absolute path to our ghostscript files 
     var path = HostingEnvironment.MapPath("~/App_Data/Ghostscript"); 

     // Set our ghostscript directory 
     MagickNET.SetGhostscriptDirectory(path); 

     // Assign our container to our controller 
     var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureStorage"].ConnectionString); 
     var blocClient = storageAccount.CreateCloudBlobClient(); 
     this.container = blocClient.GetContainerReference("kudos-sports"); 

     // Create the container if it doesn't already exist 
     container.CreateIfNotExists(); 

     // Give public access to the blobs 
     container.SetPermissions(new BlobContainerPermissions 
     { 
      PublicAccess = BlobContainerPublicAccessType.Blob 
     }); 
    } 

    /// <summary> 
    /// Uploads an image to the azure storage container 
    /// </summary> 
    /// <param name="provider">The multipart memorystream provider</param> 
    /// <returns></returns> 
    public async Task<string> UploadImageAsync(MultipartMemoryStreamProvider provider) 
    { 

     // Read our content 
     using (var content = provider.Contents.FirstOrDefault()) 
     { 

      // If the content is empty, throw an error 
      if (content == null) 
       throw new Exception("The file was not found."); 

      // Create a new blob block to hold our image 
      var extension = this.GetFileExtension(content); 
      var blockBlob = container.GetBlockBlobReference(Guid.NewGuid().ToString() + extension); 

      // Upload to azure 
      var stream = await content.ReadAsStreamAsync(); 
      await blockBlob.UploadFromStreamAsync(stream); 

      // Return the blobs url 
      return blockBlob.StorageUri.PrimaryUri.ToString(); 
     } 
    } 

    public async Task<string> ConvertImage(string path) 
    { 

     // Create our web client 
     using (var client = new WebClient()) 
     { 

      // Get our data as bytes 
      var data = client.DownloadData(path); 

      // Create our image 
      using (var image = new MagickImage(data)) 
      { 

       // Create a new memory stream 
       using (var memoryStream = new MemoryStream()) 
       { 

        // Set to a png 
        image.Format = MagickFormat.Png; 
        image.Write(memoryStream); 

        // Create a new blob block to hold our image 
        var blockBlob = container.GetBlockBlobReference(Guid.NewGuid().ToString() + ".png"); 

        // Upload to azure 
        await blockBlob.UploadFromStreamAsync(memoryStream); 

        // Return the blobs url 
        return blockBlob.StorageUri.PrimaryUri.ToString(); 
       } 
      } 
     } 
    } 

    /// <summary> 
    /// Deletes and image from the azure storage container 
    /// </summary> 
    /// <param name="name">The name of the file to delete</param> 
    public void DeleteImage(string name) 
    { 

     // Get our item 
     var blockBlob = container.GetBlockBlobReference(name); 

     // Delete the item 
     blockBlob.Delete(); 
    } 

    /// <summary> 
    /// Gets the extension from a file 
    /// </summary> 
    /// <param name="content">The HttpContent of an uploaded file</param> 
    /// <returns></returns> 
    private string GetFileExtension(HttpContent content) 
    { 

     // Get our filename 
     var filename = content.Headers.ContentDisposition.FileName.Replace("\"", string.Empty); 

     // Split our filename 
     var parts = filename.Split('.'); 

     // If we have any parts 
     if (parts.Length > 0) 
     { 

      // Get our last part 
      var extension = parts[parts.Length - 1]; 

      // Return our extension 
      return "." + extension; 
     } 

     // If we don't have an extension, mark as png 
     return ".png"; 
    } 
} 

这是转换方法,似乎有这个问题,但我想不出是什么问题。 有谁知道这个问题可能是什么?

回答

3

你把它上传到蔚蓝之前,您应该移动存储流的位置回到开始:

image.Write(memoryStream); 

// Create a new blob block to hold our image 
var blockBlob = container.GetBlockBlobReference(Guid.NewGuid().ToString() + ".png"); 

// Upload to azure 
memoryStream.Position = 0; 
await blockBlob.UploadFromStreamAsync(memoryStream); 

附:请注意,如果您想将其用于商业产品(http://ghostscript.com/doc/9.16/Commprod.htm),则需要Ghostscript的许可证。