Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6940061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:42:19+00:00 2026-05-27T12:42:19+00:00

I have the following code in my IHttpHandler: Dim MemoryStream1 As New System.IO.MemoryStream MemoryStream1.Write(SqlDataReader1(cover),

  • 0

I have the following code in my IHttpHandler:

            Dim MemoryStream1 As New System.IO.MemoryStream

            MemoryStream1.Write(SqlDataReader1("cover"), 0, SqlDataReader1("cover").Length - 1)

            Dim Bitmap1 As System.Drawing.Bitmap = System.Drawing.Bitmap.FromStream(MemoryStream1)

            Dim Width1 As Integer = Bitmap1.Width
            Dim Height1 As Integer = Bitmap1.Height

            Dim Width2 As Integer = 90
            Dim Height2 As Integer = Height1 * Width1 / Width1

            Dim Bitmap2 As System.Drawing.Bitmap = New System.Drawing.Bitmap(Width2, Height2)

            Dim Graphics1 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(Bitmap2)

            Graphics1.DrawImage(Bitmap1, 0, 0, Width2, Height2)

            Dim MemoryStream2 As New System.IO.MemoryStream

            Bitmap2.Save(MemoryStream2, System.Drawing.Imaging.ImageFormat.Png)

            context.Response.BinaryWrite(MemoryStream2.ToArray)

It works but I’m not sure that it is the right way to resize an image. How to simplify that code?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:42:19+00:00Added an answer on May 27, 2026 at 12:42 pm

    Basically the code is correct, but there are some problems with it:

    • You are skipping the last byte when you write into the first memory stream. The last property in the Write call should be the length, not the length minus one.
    • Your calculation of Height2 is incorrect. The expression Height1 * Width1 / Width1 will always evaluate to the value of Height1. You should use Height1 * Width2 / Width1 instead.
    • You are not disposing your memory streams, bitmaps or graphics objects. Use Using blocks to make sure that the objects are disposed.

    You can simplify the code somewhat by creating the first memory stream from the byte array instead of writing the array to the stream:

    Using MemoryStream1 As New System.IO.MemoryStream(SqlDataReader1("cover"))
    
      Using Bitmap1 As System.Drawing.Bitmap = System.Drawing.Bitmap.FromStream(MemoryStream1)
    
        Dim Width1 As Integer = Bitmap1.Width
        Dim Height1 As Integer = Bitmap1.Height
    
        Dim Width2 As Integer = 90
        Dim Height2 As Integer = Height1 * Width2 / Width1
    
        Using Bitmap2 As System.Drawing.Bitmap = New System.Drawing.Bitmap(Width2, Height2)
    
          Using Graphics1 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(Bitmap2)
    
            Graphics1.DrawImage(Bitmap1, 0, 0, Width2, Height2)
    
          End Using
    
          Using MemoryStream2 As New System.IO.MemoryStream
    
            Bitmap2.Save(MemoryStream2, System.Drawing.Imaging.ImageFormat.Png)
    
            context.Response.BinaryWrite(MemoryStream2.ToArray)
    
          End Using
    
        End Using
    
      End Using
    
    End Using
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code class User attr_accessor :name end u = User.new u.name =
I have following code snipped: ... var tpc = new ThirtPartyClass(); tpc.ExecuteCommand(); tpc.ExecuteCommand(); ...
I have following code in C# PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
I have following code in C# PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
I have following code snippet in c#. var list = new List<string> { a,
I have the following code: Unity Container: Settings settings = CreateSettings(); container.RegisterInstance(settings) .RegisterType<MyHttpHandler>(new InjectionProperty[]
I have following code: $telnums = array(10, 20, 30); $obj = new StdClass(); $obj->telnums
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.