RadBinaryImage save a copy

5 posts, 0 answers
  1. Emiliano
    Emiliano avatar
    8 posts
    Member since:
    Aug 2013

    Posted 17 Sep 2013 Link to this post

    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?
  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 20 Sep 2013 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Emiliano
    Emiliano avatar
    8 posts
    Member since:
    Aug 2013

    Posted 20 Sep 2013 Link to this post

    Thank you very much Konstantin . I had not thought of this solution
  5. Emiliano
    Emiliano avatar
    8 posts
    Member since:
    Aug 2013

    Posted 20 Sep 2013 Link to this post

    In this way I get an image with the original size of the file and not 150 as I had set for 150

    thanks!
  6. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 25 Sep 2013 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017