Answered by:
Converting svg file to image

Question
-
User514976284 posted
Hi All,
I am using AsyncFileUpload controls to upload jpg, jpeg, png, gif and svg file and resize the image to display as thumbnail on the application screen. I am able to resize all the image formats except svg.
In AsyncFileUpload_UploadedComplete Method I am calling a function that will resize the original image to Thumbnail.
Calling method:
GenerateThumbnail(AsyncFileUpload.PostedFile.InputStream, AsyncFileUpload.PostedFile.ContentType, thumbFilename, fileGuid);
Method:
private void GenerateDBThumbnail(Stream source, string contentType, string imageName, string fileGuid)
{
if (contentType.ToLower().IndexOf("image/") < 0) return;using (var image = Image.FromStream(source))
{// Image resize;
}
}
For svg file error hits in the line: using (var image = Image.FromStream(source)) as " Parameter is not valid".
Kindly help me on this.
Thanks,
Sasikala.
Monday, June 1, 2020 10:20 AM
Answers
-
User-821857111 posted
An svg file is not a supported Image file. It is an XML file. You could use the browser to convert it: https://stackoverflow.com/questions/3975499/convert-svg-to-image-jpeg-png-etc-in-the-browser
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 1, 2020 1:08 PM -
User514976284 posted
Hi,
Thanks for your reply. Finally I was able to achieve thumbnail for SVG image file by converting it in to png.
Implemented as per the below link.
https://dotnetfiddle.net/sY3EAs.
instead of saving as bmp, I passed the file as stream to convert to thumbnail.
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
{
bmp.Save(memory, ImageFormat.Jpeg);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}}
For this I have installed SVG package from NuGet.
Now the problem is when I check in the code in Git, it is successful, but the pipeline fails giving the below error, as this is a Website application, the package,config is checked in and build succeeded but Deployment fails. As this is a Website application the dll automatically added into bin, and package.config. So implemented in the asp.net page with Using Svg;
error CS0246: The type or namespace name 'Svg' could not be found (are you missing a using directive or an assembly reference?)
Issue resolved by pushing the Svg and its dependent dlls to Git.
Thanks,
Sasilkala.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 9, 2020 5:24 AM
All replies
-
User-821857111 posted
An svg file is not a supported Image file. It is an XML file. You could use the browser to convert it: https://stackoverflow.com/questions/3975499/convert-svg-to-image-jpeg-png-etc-in-the-browser
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 1, 2020 1:08 PM -
User514976284 posted
Hi,
Thanks for your reply. Finally I was able to achieve thumbnail for SVG image file by converting it in to png.
Implemented as per the below link.
https://dotnetfiddle.net/sY3EAs.
instead of saving as bmp, I passed the file as stream to convert to thumbnail.
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
{
bmp.Save(memory, ImageFormat.Jpeg);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}}
For this I have installed SVG package from NuGet.
Now the problem is when I check in the code in Git, it is successful, but the pipeline fails giving the below error, as this is a Website application, the package,config is checked in and build succeeded but Deployment fails. As this is a Website application the dll automatically added into bin, and package.config. So implemented in the asp.net page with Using Svg;
error CS0246: The type or namespace name 'Svg' could not be found (are you missing a using directive or an assembly reference?)
Issue resolved by pushing the Svg and its dependent dlls to Git.
Thanks,
Sasilkala.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 9, 2020 5:24 AM