locked
Image not show in crystal report RRS feed

  • Question

  • i have resize an image through  c # 200*200 pixel and save it in our sql server database but it didn't show when i view through crystal and when i do the same process manually through Ms paint it show the image.

     

    c # images converson code (which i have used)

     System.Drawing.Image image = System.Drawing.Image.FromFile(str);
                int srcWidth = image.Width;
                int srcHeight = image.Height;
                int thumbwidth, thumbheight;
                if (srcWidth > srcHeight)
                {
                    thumbwidth = 200;
                    thumbheight = ((srcHeight * 200) / srcWidth);
                }
                else
                {
                    thumbheight = 200;
                    thumbwidth = ((srcWidth * 200) / srcHeight);
                }
                Bitmap bmp = new Bitmap(thumbwidth, thumbheight, PixelFormat.Format24bppRgb);
                System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
                gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbwidth, thumbheight);
                gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
                bmp.Save(dst); 

     

    Thanks

     


    Star Link Communication Pvt. Ltd. A19 Okhla, Phase-I New Delhi 110020 India
    • Moved by Leo Liu - MSFTModerator Tuesday, June 28, 2011 3:23 AM Moved for better support. (From:Visual C# Language)
    Monday, June 27, 2011 1:08 PM

Answers

  • I have the solution of problem “Image Not Shown in Crystal Report”. Just copy the below code and paste it in your web application.
    It will help you to resize your image and will show in crystal report also. All you need to copy source code in your Default.aspx page which is given below:-

    <form id="form1" runat="server">
        <div>
       
            <br />
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <br />
            <asp:TextBox ID="txtOUTpath" runat="server" Width="306px"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="btngo" runat="server" Text="Go" onclick="btngo_Click"
                Width="104px" />
            <asp:Label ID="lblCurrentSize" runat="server" Text="&lt;Size&gt;"></asp:Label>
            <asp:Label ID="lblName" runat="server" Text="&lt;Name&gt;"></asp:Label>
            <br />
            <br />

               
        </div>
        </form>


    Now paste this given code on btngo in your default.aspx.cs page

    protected void btngo_Click(object sender, EventArgs e)
        {
            try
            {
                CompressMergImg();
               
            }
            catch
            {
            }
        }
        private void CompressMergImg()
        {
            string Fshort = FileUpload1.FileName;
            FileUpload1.SaveAs(Server.MapPath("Img").ToString() + @"\" + FileUpload1.FileName);
            string Ffull = Server.MapPath(@"~\Img\") + Fshort.ToString();

            FromFile(Ffull);                                                              // Function I   "FromFile"              
            Reduce(double.Parse("0.6", new System.Globalization.CultureInfo("EN-us")));   // Function II  "Reduce"
            ImgPath = Server.MapPath(@"~\Img\") + Fshort.ToString();
            ToFile(Ffull);                                                                // Function III "ToFile"
            CompressImg = ImgPath;
            MergeImg();                                                                   // Function IV  "MergeImg","Makewhitescale"
        }
        private void FromFile(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            byte[] imgData = new byte[fs.Length + 1];
            fs.Read(imgData, 0, (int)fs.Length);
            fs.Close();
            try
            {
                img = (System.Drawing.Bitmap)(Image.FromStream(new MemoryStream(imgData)));
                imgFormat = img.RawFormat;
                lblCurrentSize.Text = "Current Size: " + (imgData.Length - 1) / 1024 + "Kb (" + img.Width + "x" + img.Height + ") [" + img.Width / img.Height + "]";
            }
            catch
            { lblCurrentSize.Text = "Error"; }
        }
        private void Reduce(double factor)
        {
            double oldwidth = img.Size.Width;
            double oldheight = img.Size.Height;
            if (oldwidth > oldheight)
            {
                factor = (abcd / oldwidth);
            }
            else
            {
                factor = (abcd / oldheight);
            }
            img = new Bitmap(img, new Size(System.Convert.ToInt32(img.Size.Width * factor), System.Convert.ToInt32(img.Size.Height * factor)));
            Bitmap img1 = new Bitmap(200, 200);
            string SizeKb;      // To compute: size in Kb
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            SizeKb = (string)((ms.Length / 1024).ToString() + "Kb ");
            lblCurrentSize.Text = "Current Size: " + SizeKb + "(" + img.Width + "x" + img.Height + ") [" + img.Width / img.Height + "]";
        }
        private void ToFile(string filename)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] imgData = new byte[ms.Length - 1 + 1];
            ms.Position = 0;
            ms.Read(imgData, 0, (int)ms.Length);
            FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
            fs.Write(imgData, 0, imgData.Length - 1);
            fs.Close();
        }
        private void MergeImg()
        {
            try
            {
                Bitmap img = new Bitmap(200, 200);         //Create Image of 200 by 200
                Image imgB = MakeWhitescale(img);         //It will make the background color of image white

                System.Drawing.Graphics myGraphic = null;

                Image imgF = Image.FromFile(CompressImg);
                myGraphic = System.Drawing.Graphics.FromImage(imgB);
                int y = (Convert.ToInt32(abcd) - imgF.Height) / 2;
                int x = (Convert.ToInt32(abcd) - imgF.Width) / 2;
                myGraphic.DrawImageUnscaled(imgB, 0, 0);
                myGraphic.DrawImageUnscaled(imgF, x, y);
                myGraphic.Save();
                imgB.Save(ImgPath + "Temp.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                Response.Write("Image Resized and Merge successfully");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static Bitmap MakeWhitescale(Bitmap original)
        {
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);    //make an empty bitmap the same size as original
            for (int i = 0; i < original.Width; i++)
            {
                for (int j = 0; j < original.Height; j++)
                {
                    Color originalColor = original.GetPixel(i, j);       //get the pixel from the original image          
                    int whiteScale = 255;                                //create the grayscale version of the pixel               
                    Color newColor = Color.FromArgb(whiteScale, whiteScale, whiteScale);  //create the color object
                    newBitmap.SetPixel(i, j, newColor);                 //set the new image's pixel to the grayscale version
                }
            }
            return newBitmap;
        }


    That’s It. But don’t forget to add a  “Img” folder in your web application. Your resized image will store in “Img” folder …. Check this code and let me know did it work for you  or not……

    Saturday, July 23, 2011 10:24 AM

All replies

  • How do you save the Bitmap into the database, and how do you get it from the database to the Crystal Report?
    What is the "dst" here?

    By the way, I am moving your thread into the Crystal Report Forum for specialized support. Thanks.


    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 28, 2011 3:22 AM
    Moderator
  • First of all we resize image using above code seprately...

     And dst use in above project is the file path where we save the image after resize.

    After that
    We select file using file uploader control in C#.Net

    if (FileUpload1.HasFile)
            {
                string ServerPath = Server.MapPath(@"~\TempImg\" + Session["UserId"].ToString() + ".Jpg");
                FileUpload1.SaveAs(ServerPath);

                ItemInfo info = new ItemInfo();
                info.ItemCode = TxtCode.Text;
                FileStream fs = new FileStream(Server.MapPath(@"~\TempImg\" + Session["UserId"].ToString() + ".Jpg"), FileMode.Open, FileAccess.Read);
                byte[] myimage = new byte[fs.Length - 1];
                fs.Read(myimage, 0, myimage.Length);
                fs.Close();
                info.ImageFile = myimage;

                SqlConnection Cn = Connection.conn();
                SqlCommand CmdInsert;
                Cn.Open();
                CmdInsert = new SqlCommand("update MstItem set ImageFile=@Image Where Code=@ItemCode", Cn);

                CmdInsert.Parameters.Add(new SqlParameter("@ItemCode", SqlDbType.NVarChar, 15));
                CmdInsert.Parameters["@ItemCode"].Value = info.ItemCode;
                CmdInsert.Parameters.Add("@Image", SqlDbType.Image, info.ImageFile.Length).Value = info.ImageFile;
                CmdInsert.ExecuteNonQuery();
                Cn.Close();
            }

    For Show image in Crystal Report...
    We first design XML Schema, and define fields as
    <xs:element name="ImageFile" type="xs:base64Binary" minOccurs="0" />

    After that we
                ReportDocument RptDoc = new ReportDocument(); 
                RptDoc.Load(Server.MapPath("../RPT/SalesProposal.rpt"));
                DataSet ds = new DataSet();
               
                ds = ObjReport.SalesProposal(Session["ReportCode"].ToString().Trim());
                ds.ReadXmlSchema(Server.MapPath("../RPTSchema/SalesProposal.xsd"));
                RptDoc.SetDataSource(ds);
                CrystalReportViewer1.ReportSource = RptDoc;

     

           public DataSet SalesProposal(string WhereClause)
            {
                SqlConnection Conn = Connection.conn();
                Conn.Open();
                SqlCommand Cmd = new SqlCommand("RptSalesProposal", Conn);
                Cmd.CommandType = CommandType.StoredProcedure;
                Cmd.Parameters.Add("@WhereClause", SqlDbType.VarChar, 5000).Value = WhereClause;
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(Cmd);
                da.Fill(ds, "SalesProposal");
                Conn.Close();
                return ds;
            }

     


    Star Link Communication Pvt. Ltd. A19 Okhla, Phase-I New Delhi 110020 India
    Tuesday, June 28, 2011 6:16 AM
  • What version of VS and CR are you using?

    Don


    Don Williams
    Tuesday, June 28, 2011 6:19 PM
  • We are using VS 2008 and CR crystal report basic for visual studio 2008 10.5.0.0
    Star Link Communication Pvt. Ltd. A19 Okhla, Phase-I New Delhi 110020 India
    Wednesday, June 29, 2011 4:11 AM
  • I am waiting for reply...

     

    Please look into matter.

     


    Star Link Communication Pvt. Ltd. A19 Okhla, Phase-I New Delhi 110020 India
    Friday, July 1, 2011 5:45 AM
  • Hi Vipin,

             Can you ensure using debugger that your dataset can retrive imaze data by xml..?
             if it can contain data, then you can check your dataset type which you use for crystal report.
    Regards atik sarker
    Friday, July 8, 2011 6:05 AM
  • Hello,

    Sorry Forums stopped notifying me and I could not get it to work until I used a different e-mail address.

    You cannot add an image at runtime in CR Basic. Create a new report and insert an Image or OLE Object, select the field from the database or create the report off the XML file and drop the image field onto the report.

    To be able to add the image at runtime you have to use RAS ( report application server )

    Don


    Don Williams
    Friday, July 22, 2011 8:21 PM
  • I have the solution of problem “Image Not Shown in Crystal Report”. Just copy the below code and paste it in your web application.
    It will help you to resize your image and will show in crystal report also. All you need to copy source code in your Default.aspx page which is given below:-

    <form id="form1" runat="server">
        <div>
       
            <br />
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <br />
            <asp:TextBox ID="txtOUTpath" runat="server" Width="306px"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="btngo" runat="server" Text="Go" onclick="btngo_Click"
                Width="104px" />
            <asp:Label ID="lblCurrentSize" runat="server" Text="&lt;Size&gt;"></asp:Label>
            <asp:Label ID="lblName" runat="server" Text="&lt;Name&gt;"></asp:Label>
            <br />
            <br />

               
        </div>
        </form>


    Now paste this given code on btngo in your default.aspx.cs page

    protected void btngo_Click(object sender, EventArgs e)
        {
            try
            {
                CompressMergImg();
               
            }
            catch
            {
            }
        }
        private void CompressMergImg()
        {
            string Fshort = FileUpload1.FileName;
            FileUpload1.SaveAs(Server.MapPath("Img").ToString() + @"\" + FileUpload1.FileName);
            string Ffull = Server.MapPath(@"~\Img\") + Fshort.ToString();

            FromFile(Ffull);                                                              // Function I   "FromFile"              
            Reduce(double.Parse("0.6", new System.Globalization.CultureInfo("EN-us")));   // Function II  "Reduce"
            ImgPath = Server.MapPath(@"~\Img\") + Fshort.ToString();
            ToFile(Ffull);                                                                // Function III "ToFile"
            CompressImg = ImgPath;
            MergeImg();                                                                   // Function IV  "MergeImg","Makewhitescale"
        }
        private void FromFile(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            byte[] imgData = new byte[fs.Length + 1];
            fs.Read(imgData, 0, (int)fs.Length);
            fs.Close();
            try
            {
                img = (System.Drawing.Bitmap)(Image.FromStream(new MemoryStream(imgData)));
                imgFormat = img.RawFormat;
                lblCurrentSize.Text = "Current Size: " + (imgData.Length - 1) / 1024 + "Kb (" + img.Width + "x" + img.Height + ") [" + img.Width / img.Height + "]";
            }
            catch
            { lblCurrentSize.Text = "Error"; }
        }
        private void Reduce(double factor)
        {
            double oldwidth = img.Size.Width;
            double oldheight = img.Size.Height;
            if (oldwidth > oldheight)
            {
                factor = (abcd / oldwidth);
            }
            else
            {
                factor = (abcd / oldheight);
            }
            img = new Bitmap(img, new Size(System.Convert.ToInt32(img.Size.Width * factor), System.Convert.ToInt32(img.Size.Height * factor)));
            Bitmap img1 = new Bitmap(200, 200);
            string SizeKb;      // To compute: size in Kb
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            SizeKb = (string)((ms.Length / 1024).ToString() + "Kb ");
            lblCurrentSize.Text = "Current Size: " + SizeKb + "(" + img.Width + "x" + img.Height + ") [" + img.Width / img.Height + "]";
        }
        private void ToFile(string filename)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] imgData = new byte[ms.Length - 1 + 1];
            ms.Position = 0;
            ms.Read(imgData, 0, (int)ms.Length);
            FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
            fs.Write(imgData, 0, imgData.Length - 1);
            fs.Close();
        }
        private void MergeImg()
        {
            try
            {
                Bitmap img = new Bitmap(200, 200);         //Create Image of 200 by 200
                Image imgB = MakeWhitescale(img);         //It will make the background color of image white

                System.Drawing.Graphics myGraphic = null;

                Image imgF = Image.FromFile(CompressImg);
                myGraphic = System.Drawing.Graphics.FromImage(imgB);
                int y = (Convert.ToInt32(abcd) - imgF.Height) / 2;
                int x = (Convert.ToInt32(abcd) - imgF.Width) / 2;
                myGraphic.DrawImageUnscaled(imgB, 0, 0);
                myGraphic.DrawImageUnscaled(imgF, x, y);
                myGraphic.Save();
                imgB.Save(ImgPath + "Temp.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                Response.Write("Image Resized and Merge successfully");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static Bitmap MakeWhitescale(Bitmap original)
        {
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);    //make an empty bitmap the same size as original
            for (int i = 0; i < original.Width; i++)
            {
                for (int j = 0; j < original.Height; j++)
                {
                    Color originalColor = original.GetPixel(i, j);       //get the pixel from the original image          
                    int whiteScale = 255;                                //create the grayscale version of the pixel               
                    Color newColor = Color.FromArgb(whiteScale, whiteScale, whiteScale);  //create the color object
                    newBitmap.SetPixel(i, j, newColor);                 //set the new image's pixel to the grayscale version
                }
            }
            return newBitmap;
        }


    That’s It. But don’t forget to add a  “Img” folder in your web application. Your resized image will store in “Img” folder …. Check this code and let me know did it work for you  or not……

    Saturday, July 23, 2011 10:24 AM