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

Custom RadBinaryImage HttpHandler

5 Answers 163 Views
BinaryImage
This is a migrated thread and some comments may be shown as answers.
Dwayne
Top achievements
Rank 1
Dwayne asked on 27 Dec 2010, 03:04 PM
 I have a  RadBinaryImage control and I want to implement a custom httphandeler but I am having a bit of trouble doing this task. The reason for my custom implementation is that  I have images stored in my DB as binary  that are 100kb in size I have a page which displays up to  170 of these images as thumbnail size but the are still 100kb in size. So at times when the page is rendered some images will not show.  
 
I tried using the AutoAdjustImageControlSize  and  ResizeMode and had not acheived  the desired effect.  I may be going about this the hard way but i wrote a handler to reduce and resize the images and spit the binary out but I can't seem to get it to work.   This is my handeler
Public Class RenderImageHandler
    Implements System.Web.IHttpHandler
  
    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
  
        Dim Response As HttpResponse = context.Response
        '  Dim ImageUrl As bi = HttpContext.Current.Server.MapPath("~/01SC087.JPG")
   
        Dim imgLoader As New PService
        Dim id As Integer
        Integer.TryParse(context.Request.QueryString("i_id"), id)
        If id > 0 Then
   
            Dim ic As New ImageConverter()
            Dim imgN As Image = DirectCast(ic.ConvertFrom(imgLoader._GetPostingImg(id)), Image)
            Dim bitmap1 As New Bitmap(imgN)
            Dim thumbnailer As New ImageThumbnailer()
  
            Dim bmp As System.Drawing.Bitmap = thumbnailer.CreateThumbnail(bitmap1, 150, 150, True)
            Dim ms As New System.IO.MemoryStream()
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
            Dim imageData As Byte() = ms.GetBuffer()
  
            Response.ContentType = "image/jpeg"
            context.Response.BinaryWrite(imageData)
            context.Response.Flush()
  
        End If
    End Sub
  
    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
  
  
  
End Class



and this is my  ImageThumbnailer  class

Public Class ImageThumbnailer
    Private _thumb As Bitmap = Nothing
  
       
    Public Function CreateThumbnail(ByVal SourceImage As Bitmap, ByVal Width As Int32, ByVal Height As Int32, ByVal KeepRatio As [Boolean]) As Bitmap
        ' if Source Bitmap smaller than designated thumbnail => Return Original
        If SourceImage.Width < Width AndAlso SourceImage.Height < Height Then
            Return SourceImage
        End If
  
        Try
            Dim _Width As Int32 = 0
            Dim _Height As Int32 = 0
  
            _Width = Width
            _Height = Height
  
            If KeepRatio Then
                If SourceImage.Width > SourceImage.Height Then
                    _Width = Width
                    _Height = CType(SourceImage.Height * (Width / SourceImage.Width), Int32)
                Else
                    _Height = Height
                    _Width = CType(SourceImage.Width * (Height / SourceImage.Height), Int32)
                End If
            End If
  
            _thumb = New Bitmap(_Width, _Height)
            Using g As Graphics = Graphics.FromImage(_thumb)
                g.InterpolationMode = InterpolationMode.HighQualityBicubic
                g.FillRectangle(Brushes.White, 0, 0, _Width, _Height)
                g.DrawImage(SourceImage, 0, 0, _Width, _Height)
            End Using
        Catch
            _thumb = Nothing
        End Try
        Return _thumb
    End Function
  
      
End Class



I f i am going about this the wrong way please let me know. I want the thumb nails to be less bytes when it is rendered to the page which will increase the speed  and resolve the issue of images not being rendered.  The page in question is at the current url @  http://www.4dpeeps.com 

5 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 30 Dec 2010, 04:16 PM
Hello Dwayne,

I am not sure what you are aiming by creating custom handler for RadBinaryImage, but for your convenience I've prepared sample demonstrating how to create such. Please find the attachment.

Regards,
Nikolay
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Dwayne
Top achievements
Rank 1
answered on 30 Dec 2010, 06:05 PM
My aim is to speed up loading of the images while reducing the size of the original image. If there is a better way than the one I would appreciate a point in the right direction.
0
Cori
Top achievements
Rank 2
answered on 30 Dec 2010, 09:11 PM
Hello Dwayne,

From my experience, if you set the ResizeMode to Fit or Crop and set the width/height of the RadBinaryImage control, it will do the image resizing for you on the server and only send the resized image to the browser.
0
Dwayne
Top achievements
Rank 1
answered on 03 Jan 2011, 06:58 AM
I tried that and it didn't give me the desired result. One of the biggest problem is pag speed. I have been using firebug for firefox to analyze the page speed and based on the results my site is performing slow and telerik webresource is hogging the page. I am trying to find a way to speed the entire process. I have CDN enabled and im trying to combine and compress all css and javascript but that needs  alittle work. I want to cache telerik resouces for longer periods but haven't found away to make this happen.
0
Nikolay Rusev
Telerik team
answered on 06 Jan 2011, 08:31 AM
Hello Dwayne,

Our web resource handler are used for server all the script/skins of the controls and by default for streaming the image from RadBinaryImage to the browser.

You should benefit by using CDN, but the content of the images must be downloaded at some point. However the response from default binary image handler caches the output for 2 hours (SetCacheExpires(DateTime.Now.AddHours(2))).

Greetings,
Nikolay
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
BinaryImage
Asked by
Dwayne
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Dwayne
Top achievements
Rank 1
Cori
Top achievements
Rank 2
Share this question
or