Trying to convert Byte[] to bitmap but giving error saying "parameter is not valid" in c#

public class PrintPage
{        
    public void buildPdf(string url)
    {
        Bitmap bmp = PrintHelpPage(url);
        Document dc = new Document();
        PdfWriter pdfWrt = PdfWriter.GetInstance(dc, new FileStream(@"D:/Experiment/Sample.pdf", FileMode.Create));
        dc.Open();
        iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(bmp, System.Drawing.Imaging.ImageFormat.Jpeg);
        dc.Add(pdfImage);
        dc.Close();
    }
    private Bitmap PrintHelpPage(string url)
    {
        if (url.ToUpper()=="DEFAULT")
        {
            url = @"https://www.google.com";
        }
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        System.Text.Encoding Enc = System.Text.Encoding.GetEncoding(response.CharacterSet);
        StreamReader sr = new StreamReader(response.GetResponseStream(), Enc); 
        string sDoc = sr.ReadToEnd();               
        sr.Close();            
        byte[] by = Encoding.ASCII.GetBytes(sDoc);
        Bitmap bm = ByteToImage(by);            
        return bm;            
    }
    public static Bitmap ByteToImage(byte[] blob)
    {
        using (MemoryStream mStream = new MemoryStream())
        {
            mStream.Write(blob, 0, blob.Length);
            mStream.Seek(0, SeekOrigin.Begin);

            Bitmap bm = new Bitmap(mStream);
            return bm;
        }
    }
}
Jon Skeet
people
quotationmark

EDIT: Subsequent to your comment:

actually I am trying to capture whole page as a picture of a random website to convert as PDF

Then you're going about it the wrong way. You'll need to start a browser (e.g. a System.Windows.Forms.WebBrowser) and somehow do a screen capture. This will be non-trivial. It's also important that you understand why your current approach doesn't work - it suggests a fundamental misunderstanding of how the web works.

Original answer

This is your most fundamental problem:

System.Text.Encoding Enc = System.Text.Encoding.GetEncoding(response.CharacterSet);
StreamReader sr = new StreamReader(response.GetResponseStream(), Enc); 
string sDoc = sr.ReadToEnd();               
sr.Close();            
byte[] by = Encoding.ASCII.GetBytes(sDoc);

You're reading an image as if it were a text file. It won't be. You'll be losing data like this.

Additionally, you're closing the memory stream that you're passing into the Bitmap constructor - you shouldn't do that.

You should just copy the response stream directly into a MemoryStream, and use that for the Bitmap:

MemoryStream stream = new MemoryStream();
using (var input = response.GetResponseStream())
{
    input.CopyTo(stream);
}
stream.Position = 0;
Bitmap bitmap = new Bitmap(stream);

Oh, and you should also use a using statement for the response, otherwise that won't get disposed, which can cause timeouts for future requests due to connection pooling.

people

See more on this question at Stackoverflow