tags:

views:

105

answers:

0

Hello!

I recently learned how to put the Image in canvas from code behind, i took the width and height of all images from database at run time. the margin of all the images is the same as i want but they do not look parallel. Please help me figure out how can achieve it.

Thanks in advance! Best regards, shashank

private void LoadPreview()
    {
        DataSet xmlData = new DataSet();
        try
        {
            xmlData.ReadXml(Utils.xmlPath);
            //get the template height and width
            DataRow[] templateRow = xmlData.Tables["M_TEMPLATE_DIMENSIONS"].Select("FK_EVENT_ID=" + CommonUtils.eventId);
            if (templateRow.Length > 0)
            {
                wallChartHeight = int.Parse(templateRow[0]["TEMPLATE_HEIGHT"].ToString());
                wallChartWidth = int.Parse(templateRow[0]["TEMPLATE_WIDTH"].ToString());
                workCanvas.Height = wallChartHeight * sizeFraction;
                workCanvas.Width = wallChartWidth * sizeFraction;

                //header image dimensions                    
                double headImgWidth = int.Parse(templateRow[0]["HEADER_IMAGE_WIDTH"].ToString());
                double headImgHeight = int.Parse(templateRow[0]["HEADER_IMAGE_HEIGHT"].ToString());
                double headImgLeft = int.Parse(templateRow[0]["HEADER_IMAGE_LEFT_MARGIN"].ToString());
                double headImgTop = int.Parse(templateRow[0]["HEADER_IMAGE_TOP_MARGIN"].ToString());
                if (headImgHeight != 0 && headImgWidth != 0)
                {
                    System.Windows.Controls.Image imgHeader = new System.Windows.Controls.Image();

                    imgHeader.Height = headImgHeight * sizeFraction;
                    imgHeader.Width = headImgWidth * sizeFraction;
                    imgHeader.SetValue(Canvas.TopProperty, headImgTop * sizeFraction);
                    imgHeader.SetValue(Canvas.LeftProperty, headImgLeft * sizeFraction);

                    workCanvas.Children.Add(imgHeader);
                }

                //large image dimensions
                double largeImgWidth = int.Parse(templateRow[0]["LARGE_IMAGE_WIDTH"].ToString());
                double largeImgHeight = int.Parse(templateRow[0]["LARGE_IMAGE_HEIGHT"].ToString());
                double largeImgLeft = int.Parse(templateRow[0]["LARGE_IMAGE_LEFT_MARGIN"].ToString());
                double largeImgTop = int.Parse(templateRow[0]["LARGE_IMAGE_TOP_MARGIN"].ToString());

                if (largeImgHeight != 0 && largeImgWidth != 0)
                {

                    System.Windows.Controls.Image imgLargeImage = new System.Windows.Controls.Image();
                    imgLargeImage.Cursor = Cursors.Hand;
                    imgLargeImage.Height = largeImgHeight - 80;
                    imgLargeImage.Width = largeImgWidth * sizeFraction;
                    imgLargeImage.Tag = largeImage;
                    BitmapImage largeBmp = new BitmapImage(new Uri(Utils.eventFolderPath + @"\large image.PNG"));
                    imgLargeImage.Source = largeBmp;
                    imgLargeImage.MouseLeftButtonDown += new MouseButtonEventHandler(imgLargeImage_MouseLeftButtonDown);

                    //set the image source
                    if (File.Exists(Utils.eventFolderPath + @"\large image.PNG"))
                    {
                        BitmapImage suffixText = AddBitMapImage(Utils.eventFolderPath + @"\large image.PNG");
                        imgLargeImage.Source = suffixText;

                    }
                    workCanvas.Children.Add(imgLargeImage);
                    Canvas.SetLeft(imgLargeImage, largeImgLeft);
                    Canvas.SetTop(imgLargeImage, largeImgTop + 20);

                }

                //small image1 dimensions
                double sMImg1Width = int.Parse(templateRow[0]["SM1_IMAGE_WIDTH"].ToString());
                double sMImg1Height = int.Parse(templateRow[0]["SM1_IMAGE_HEIGHT"].ToString());
                double sMImg1Left = int.Parse(templateRow[0]["SM1_IMAGE_LEFT_MARGIN"].ToString());
                double sMImg1Top = int.Parse(templateRow[0]["SM1_IMAGE_TOP_MARGIN"].ToString());

                if (sMImg1Height != 0 && sMImg1Width != 0)
                {

                    System.Windows.Controls.Image imgSmallImage1 = new System.Windows.Controls.Image();
                    imgSmallImage1.Cursor = Cursors.Hand;
                    BitmapImage smallBmp = new BitmapImage(new Uri(Utils.eventFolderPath + @"\small image.PNG"));
                    imgSmallImage1.Source = smallBmp;
                    imgSmallImage1.Height = sMImg1Height + 20;
                    imgSmallImage1.Width = sMImg1Width * sizeFraction;
                    imgSmallImage1.Tag = smallImage1;
                    imgSmallImage1.MouseLeftButtonDown += new MouseButtonEventHandler(imgSmallImage1_MouseLeftButtonDown);

                    //set the image source
                    if (File.Exists(Utils.eventFolderPath + @"\small image.PNG"))
                    {
                        BitmapImage suffixText = AddBitMapImage(Utils.eventFolderPath + @"\small image.PNG");
                        imgSmallImage1.Source = suffixText;
                    }

                    workCanvas.Children.Add(imgSmallImage1);
                    Canvas.SetLeft(imgSmallImage1, sMImg1Left);
                    Canvas.SetTop(imgSmallImage1, sMImg1Top + 20);

                }

                //small image2 dimenstions
                double sMImg2Width = int.Parse(templateRow[0]["SM2_IMAGE_WIDTH"].ToString());
                double sMImg2Height = int.Parse(templateRow[0]["SM2_IMAGE_HEIGHT"].ToString());
                double sMImg2Left = int.Parse(templateRow[0]["SM2_IMAGE_LEFT_MARGIN"].ToString());
                double sMImg2Top = int.Parse(templateRow[0]["SM2_IMAGE_TOP_MARGIN"].ToString());

                if (sMImg2Height != 0 && sMImg2Width != 0)
                {
                    System.Windows.Controls.Image imgSmallImage2 = new System.Windows.Controls.Image();
                    imgSmallImage2.Cursor = Cursors.Hand;
                    imgSmallImage2.Height = sMImg2Height + 20;
                    imgSmallImage2.Width = sMImg2Width * sizeFraction;
                    imgSmallImage2.Tag = smallImage2;
                    //imgSmallImage2.SetValue(Canvas.TopProperty, sMImg2Top * sizeFraction);
                    //imgSmallImage2.SetValue(Canvas.LeftProperty, sMImg2Left * sizeFraction);
                    BitmapImage smallBmp = new BitmapImage(new Uri(Utils.eventFolderPath + @"\small image.PNG"));
                    imgSmallImage2.Source = smallBmp;
                    imgSmallImage2.MouseLeftButtonDown += new MouseButtonEventHandler(imgSmallImage2_MouseLeftButtonDown);

                    //set the image source
                    if (File.Exists(Utils.eventFolderPath + @"\small image.PNG"))
                    {
                        BitmapImage suffixText = AddBitMapImage(Utils.eventFolderPath + @"\small image.PNG");
                        imgSmallImage2.Source = suffixText;
                    }
                    workCanvas.Children.Add(imgSmallImage2);
                    Canvas.SetLeft(imgSmallImage2, sMImg2Left);
                    Canvas.SetTop(imgSmallImage2, sMImg2Top + 80);
                }

                //small image3 dimensions

                double sMImg3Width = int.Parse(templateRow[0]["SM3_IMAGE_WIDTH"].ToString());
                double sMImg3Height = int.Parse(templateRow[0]["SM3_IMAGE_HEIGHT"].ToString());
                double sMImg3Left = int.Parse(templateRow[0]["SM3_IMAGE_LEFT_MARGIN"].ToString());
                double sMImg3Top = int.Parse(templateRow[0]["SM3_IMAGE_TOP_MARGIN"].ToString());

                if (sMImg3Height != 0 && sMImg3Width != 0)
                {
                    System.Windows.Controls.Image imgSmallImage3 = new System.Windows.Controls.Image();
                    imgSmallImage3.Cursor = Cursors.Hand;
                    imgSmallImage3.Height = sMImg3Height * sizeFraction;
                    imgSmallImage3.Width = sMImg3Width * sizeFraction;
                    imgSmallImage3.Tag = smallImage3;
                    //imgSmallImage3.SetValue(Canvas.TopProperty, sMImg3Top * sizeFraction);
                    //imgSmallImage3.SetValue(Canvas.LeftProperty, sMImg3Left * sizeFraction);
                    BitmapImage smallBmp = new BitmapImage(new Uri(Utils.eventFolderPath + @"\small image.PNG"));
                    imgSmallImage3.Source = smallBmp;
                    imgSmallImage3.MouseLeftButtonDown += new MouseButtonEventHandler(imgSmallImage3_MouseLeftButtonDown);

                    //set the image source
                    if (File.Exists(Utils.eventFolderPath + @"\small image.PNG"))
                    {
                        BitmapImage suffixText = AddBitMapImage(Utils.eventFolderPath + @"\small image.PNG");
                        imgSmallImage3.Source = suffixText;
                    }
                    workCanvas.Children.Add(imgSmallImage3);

                    Canvas.SetLeft(imgSmallImage3, sMImg3Left - 20);
                    Canvas.SetTop(imgSmallImage3, sMImg3Top - 30);
                }

                //text image dimensions
                double textImgWidth = int.Parse(templateRow[0]["TEXT_IMAGE_WIDTH"].ToString());
                double textImgHeight = int.Parse(templateRow[0]["TEXT_IMAGE_HEIGHT"].ToString());
                double textImgLeft = int.Parse(templateRow[0]["TEXT_IMAGE_LEFT_MARGIN"].ToString());
                double textImgTop = int.Parse(templateRow[0]["TEXT_IMAGE_TOP_MARGIN"].ToString());

                if (textImgHeight != 0 && textImgWidth != 0)
                {
                    System.Windows.Controls.Image imgtext = new System.Windows.Controls.Image();
                    imgtext.Height = textImgHeight;
                    imgtext.Width = textImgWidth;
                    imgtext.SetValue(Canvas.TopProperty, textImgTop - 110);
                    imgtext.SetValue(Canvas.LeftProperty, textImgLeft * sizeFraction);
                    workCanvas.Children.Add(imgtext);
                }

                double iDWidth = int.Parse(templateRow[0]["ID_IMAGE_WIDTH"].ToString());
                double iDHeight = int.Parse(templateRow[0]["ID_IMAGE_HEIGHT"].ToString());
                double iDLeft = int.Parse(templateRow[0]["ID_IMAGE_LEFT_MARGIN"].ToString());
                double iDTop = int.Parse(templateRow[0]["ID_IMAGE_TOP_MARGIN"].ToString());

                if (iDHeight != 0 && iDWidth != 0)
                {
                    System.Windows.Controls.Image imgId = new System.Windows.Controls.Image();
                    imgId.Height = iDWidth * sizeFraction;
                    imgId.Width = iDHeight * sizeFraction;
                    imgId.SetValue(Canvas.TopProperty, iDTop * sizeFraction);
                    imgId.SetValue(Canvas.LeftProperty, iDLeft * sizeFraction);

                    //set the image source

                    if (File.Exists(Utils.eventFolderPath + @"\ID.jpg"))
                    {
                        BitmapImage idImage = AddBitMapImage(Utils.eventFolderPath + @"\ID.jpg");
                        imgId.Source = idImage;
                    }
                    workCanvas.Children.Add(imgId);
                }

                //suffix image dimensions
                double suffixWidth = int.Parse(templateRow[0]["SUFFIX_TEXT_WIDTH"].ToString());
                double suffixHeight = int.Parse(templateRow[0]["SUFFIX_TEXT_HEIGHT"].ToString());
                double suffixLeft = int.Parse(templateRow[0]["SUFFIX_TEXT_LEFT_MARGIN"].ToString());
                double suffixTop = int.Parse(templateRow[0]["SUFFIX_TEXT_TOP_MARGIN"].ToString());
                double suffixJust = int.Parse(templateRow[0]["SUFFIX_TEXT_JUSTIFICATION"].ToString());

                if (suffixHeight != 0 && suffixWidth != 0)
                {
                    System.Windows.Controls.Image imgSuffix = new System.Windows.Controls.Image();
                    imgSuffix.Height = suffixHeight * sizeFraction;
                    imgSuffix.Width = suffixWidth * sizeFraction;
                    imgSuffix.SetValue(Canvas.TopProperty, suffixTop * sizeFraction);
                    imgSuffix.SetValue(Canvas.LeftProperty, suffixLeft * sizeFraction);

                    //set the image source
                    if (File.Exists(Utils.eventFolderPath + @"\Suffix.jpg"))
                    {
                        BitmapImage suffixText = AddBitMapImage(Utils.eventFolderPath + @"\Suffix.jpg");
                        imgSuffix.Source = suffixText;
                    }
                    workCanvas.Children.Add(imgSuffix);
                }

                // personalization image dimensions
                double personalTxtWidth = int.Parse(templateRow[0]["PERSONALIZATION_TEXT_WIDTH"].ToString());
                double personalTxtHeight = int.Parse(templateRow[0]["PERSONALIZATION_TEXT_HEIGHT"].ToString());
                double personalTxtLeft = int.Parse(templateRow[0]["PERSONALIZATION_TEXT_LEFT_MARGIN"].ToString());
                double personalTxtTop = int.Parse(templateRow[0]["PERSONALIZATION_TEXT_TOP_MARGIN"].ToString());
                double personalTxt = int.Parse(templateRow[0]["PERSONALIZATION_TEXT_JUSTIFICATION"].ToString());

                if (personalTxtHeight != 0 && personalTxtWidth != 0)
                {
                    System.Windows.Controls.Image imgPersonal = new System.Windows.Controls.Image();
                    imgPersonal.Height = personalTxtHeight * sizeFraction;
                    imgPersonal.Width = personalTxtWidth * sizeFraction;
                    imgPersonal.SetValue(Canvas.TopProperty, personalTxtTop * sizeFraction);
                    imgPersonal.SetValue(Canvas.LeftProperty, personalTxtLeft * sizeFraction);

                    //set the image source
                    if (File.Exists(Utils.eventFolderPath + @"\text.jpg"))
                    {
                        BitmapImage personalizeText = new BitmapImage();
                        personalizeText.BeginInit();
                        personalizeText.StreamSource = new FileStream(Utils.eventFolderPath + @"\text.jpg", FileMode.Open, FileAccess.Read);
                        //idImage.DecodePixelWidth = (int)_decodePixelWidth;
                        personalizeText.CacheOption = BitmapCacheOption.OnLoad;
                        personalizeText.EndInit();

                        //clean up the stream to avoid file access exceptions when attempting to delete images 
                        personalizeText.StreamSource.Dispose();

                        imgPersonal.Source = personalizeText;

                    }
                    workCanvas.Children.Add(imgPersonal);
                }


                double footerImgWidth = int.Parse(templateRow[0]["FOOTER_IMAGE_WIDTH"].ToString());
                double footerImgHeight = int.Parse(templateRow[0]["FOOTER_IMAGE_HEIGHT"].ToString());
                double footerImgLeft = int.Parse(templateRow[0]["FOOTER_IMAGE_LEFT_MARGIN"].ToString());
                double footerImgTop = int.Parse(templateRow[0]["FOOTER_IMAGE_TOP_MARGIN"].ToString());

                if (footerImgHeight != 0 && footerImgWidth != 0)
                {
                    System.Windows.Controls.Image imgFooter = new System.Windows.Controls.Image();
                    imgFooter.Height = footerImgHeight * sizeFraction;
                    imgFooter.Width = footerImgWidth * sizeFraction;
                    imgFooter.SetValue(Canvas.TopProperty, footerImgTop);
                    imgFooter.SetValue(Canvas.LeftProperty, footerImgLeft * sizeFraction);
                    workCanvas.Children.Add(imgFooter);
                }
            }


            workCanvas.SetValue(BorderBrushProperty, System.Windows.Media.Brushes.White);
            workCanvas.SetValue(BorderThicknessProperty, new Thickness(2));

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            if (xmlData != null)
            {
                xmlData.Dispose();
            }
        }
    }