This is a migrated thread and some comments may be shown as answers.

RadBinaryImage save a copy

4 Answers 46 Views
BinaryImage
This is a migrated thread and some comments may be shown as answers.
Emiliano
Top achievements
Rank 1
Emiliano asked on 17 Sep 2013, 09:20 AM
Hello, I have this code in cs

           var imagePath = Server.MapPath("myPath");
                  System.Drawing.Image image = System.Drawing.Image.FromFile(imagePath);
                  using (var stream = new System.IO.MemoryStream())
                  {
                      image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
MyRadBinaryImage.Width = 525
                     MyRadBinaryImage.Height = 613;
                     MyRadBinaryImage.DataValue = stream.ToArray();
                  }
 and this in aspx
<telerik:radbinaryimage  id="MyRadBinaryImage" runat="server" alternatetext="BinaryImage"  width="525" height="613" resizemode="Fit"  ClientIDMode="Static"></telerik:radbinaryimage>

now I would like to save a copy of the image on the server, how can I do?

4 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 20 Sep 2013, 10:29 AM
Hello Emilliano,

You could use the System.IO.WriteAllBytes() method, where you could pass the path and the "MyRadBinaryImage.DataValue" byte array as an arguments.

Here is the modified code for saving the image on the server:
var imagePath = Server.MapPath("image.jpg");
System.Drawing.Image image = System.Drawing.Image.FromFile(imagePath);
using (var stream = new System.IO.MemoryStream())
{
    image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
    MyRadBinaryImage.Width = 525;
    MyRadBinaryImage.Height = 613;
    MyRadBinaryImage.DataValue = stream.ToArray();
     
    System.IO.File.WriteAllBytes(MapPath("imageCopy.jpg"), MyRadBinaryImage.DataValue);
}

Hope that helps.

 

Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Emiliano
Top achievements
Rank 1
answered on 20 Sep 2013, 11:00 AM
Thank you very much Konstantin . I had not thought of this solution
0
Emiliano
Top achievements
Rank 1
answered on 20 Sep 2013, 02:38 PM
In this way I get an image with the original size of the file and not 150 as I had set for 150

thanks!
0
Konstantin Dikov
Telerik team
answered on 25 Sep 2013, 06:52 AM
Hello Emiliano,

The behavior you are describing is expected, since the "RadBinaryImage" "Width" and "Height" properties are relevant for the rendering of the image only and the "DataValue" will remain the same even after changing those properties.

In order to save the image with different size, you could use the approach shown bellow:
var imagePath = Server.MapPath("image.jpg");
System.Drawing.Image image = System.Drawing.Image.FromFile(imagePath);
using (var stream = new System.IO.MemoryStream())
{
    image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
    MyRadBinaryImage.DataValue = stream.ToArray();
    MyRadBinaryImage.Width = Unit.Pixel(150);
    MyRadBinaryImage.Height = Unit.Pixel(150);
}
 
using (var stream = new System.IO.MemoryStream())
{
    System.Drawing.Image resizedImage = (System.Drawing.Image)(new Bitmap(image, new Size(150, 150)));
    resizedImage.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
    System.IO.File.WriteAllBytes(MapPath("imageCopy.jpg"), stream.ToArray());
}

Hope that helps.

 

Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
BinaryImage
Asked by
Emiliano
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Emiliano
Top achievements
Rank 1
Share this question
or