0

I have uploaded an image to Azure Fileshare using asp.net and vb.net. After uploading a file I am trying to crop the image by using dimensions. I got an error with Illegal characters in path. The code works when the image is in the application's root folder. But it fails when the image is in a file share. I have tried many solutions none of them worked. Please help to resolve this. The following is the code I am using.

Using OriginalImage As SD.Image = SD.Image.FromFile(<Image Url from Azure FileShare with SAS toeken>)
                Using bmp As SD.Bitmap = New SD.Bitmap(Width, Height)
                    bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution)

                    Using Graphic As SD.Graphics = SD.Graphics.FromImage(bmp)
                        Graphic.SmoothingMode = SmoothingMode.AntiAlias
                        Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic
                        Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality
                        Graphic.DrawImage(OriginalImage, New SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel)
                        Dim ms As MemoryStream = New MemoryStream()
                        bmp.Save(ms, OriginalImage.RawFormat)
                        Return ms.GetBuffer()
                    End Using
                End Using
            End Using 

I have tried decoding the URL. Unescape characters in the string. Those didn't work.

Error I am geting "Illegal characters in path" My URL path is as follows

enter image description here

I have tried decode URL I have tried to unescape URL encoding

I need to crop an image which is already uploaded to Azure FileShare. I am using System.Drawing. If any code is to crop using Azure.Storage.Files.Shares namespace directly would be more helpful.

2 Answers2

0

What is SD.Image.FromFile? I assume this is an API provided by either .NET or by some 3rd party library you are using. I think this FromFile expects a path to a file on the user’s machine, rather than being able to handle a FileREST reference, which is something which is proprietary Azure Files thing. If you want to use the FileREST protocol, their library would need to support the FileREST protocol, either directly or through an Azure SDK, which I’m guessing it doesn’t.

Azure Files isn’t to provide the FileREST API, it’s to provide native file system APIs like SMB and NFS. To use a library which expects a path to a local file system, simply mount the Azure file share using SMB (or NFS, but I’m guessing you want SMB), and provide the path to share. For example, if the share is mounted on the M: drive, the would be M:\path\to\file.png.

Here are the instructions on how to mount an SMB share on Windows: https://learn.microsoft.com/en-us/azure/storage/files/storage-how-to-use-files-windows#mount-the-azure-file-share

  • I have resolved the issue by doing some changes in code. But the approach you suggested it should work. Bow is code worked for me. I did crop from file share – Okta Canvas Mar 30 '23 at 05:16
0

I have made changes as following. Converting image into memorystream and passing memory stream to SD.Image.FromStream

Dim ImageAsMemoryStream As MemoryStream = GetTempProfileImageAsMemoryStream(Img)
            Using OriginalImage As SD.Image = SD.Image.FromStream(ImageAsMemoryStream)
                Using bmp As SD.Bitmap = New SD.Bitmap(Width, Height)
                    bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution)

                    Using Graphic As SD.Graphics = SD.Graphics.FromImage(bmp)
                        Graphic.SmoothingMode = SmoothingMode.AntiAlias
                        Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic
                        Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality
                        Graphic.DrawImage(OriginalImage, New SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel)
                        Dim ms As MemoryStream = New MemoryStream()
                        bmp.Save(ms, OriginalImage.RawFormat)
                        Return ms.GetBuffer()
                    End Using
                End Using
            End Using




rivate Shared Function GetTempProfileImageAsMemoryStream(ByVal ImageName As String)

        Dim connectionString As String = <azure file share connection string> 
        Dim shareName As String = "AzureFileShareName"
        Dim dirName As String ="AzureFileShareDirectory"
        Dim fileName As String = ImageName
        Dim share As ShareClient = New ShareClient(connectionString, shareName)
        Dim directory As ShareDirectoryClient = share.GetDirectoryClient(dirName)
        Dim file As ShareFileClient = directory.GetFileClient(fileName)
        Dim download As ShareFileDownloadInfo = file.Download()
        Dim memoryStream As MemoryStream = New MemoryStream()

        Using downloadStream As Stream = download.Content
            downloadStream.CopyTo(memoryStream)
        End Using


        Return memoryStream

    End Function

This code worked for me