how to set max width on picture control - InfoPath Dev
in

InfoPath Dev

how to set max width on picture control

Last post 06-07-2012 06:25 AM by Gjermund. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 06-05-2012 07:08 AM

    how to set max width on picture control

    Hi,

     I have a view with a picture control where height and width are set to auto. The users can insert a picture in that control and everything is fine until they try to print. I have a separate view for printing (also with a picture control) and if the picture is not wider than a printer page it will print ok, but if it is a wide picture it will cut of the right part when printing. The only thing I found that helps is setting picture properties to a pre set width and keep height on auto. That scales down wide images, but small images are also scaled up. The scaling up is not wanted. Only scale down. So basically what i want is a maxwith setting I can use. Any sugestions on how to fix this?

    -Gjermund

  • 06-06-2012 07:12 AM In reply to

    Re: how to set max width on picture control

     If we had a way of knowing the picture's dimensions ahead of time, we could have TWO image controls. Show one of them if the image is wider than 200 pixels (for example) and show the other (for smaller images, with no pre-set width) if the image is smaller than 200 pixels. Then it's a matter of hiding one of those image controls and showing the other, according to the picture's dimensions...

    Just a thought...

    Ernesto Machado
    Qdabra® Software/ InfoPathDev.com
    The InfoPath Experts – Streamline data gathering to turn process into knowledge.™


  • 06-07-2012 06:25 AM In reply to

    Re: how to set max width on picture control

    If we only had a way...

    I ended up with a solution similar to what Ernesto suggested. I use one picture control for the screen view and another for the print view. When the screen view control changes I added some custom code to check and scale the print version if needed. Now I just have to do the same for rich textboxes with images in them :( . For anyone with similar problem the code is as follows (error handling not added yet) :

            public void PurposeOverviewIMG_Changed(object sender, XmlEventArgs e)
            {
                CreatePrintImage("/my:myFields/my:View_Purpose/my:PurposeOverviewIMG", "/my:myFields/my:View_Purpose/my:PurposeOverviewIMGPrint", 620);
            }

            private void CreatePrintImage(string sourceXPath, string targetXPath, int maxWidth)
            {
                XPathNavigator document = this.MainDataSource.CreateNavigator();
                XPathNavigator imageNode = document.SelectSingleNode(sourceXPath, this.NamespaceManager);
                XPathNavigator imgCopy = document.SelectSingleNode(targetXPath, this.NamespaceManager);

                string imageString = imageNode.Value;
                if (imageString.Length == 0)
                {
                    //If deleted remove print copy
                    XPathNavigator imgAttr = imgCopy.Clone();
                    bool b = imgAttr.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance");
                    imgCopy.SetValue("");
                    imgAttr.SetValue("true");
                    return;
                }
               
                byte[] imageDecoded = Convert.FromBase64String(imageString);
                using (MemoryStream imageMemStream = new MemoryStream(imageDecoded))
                using (Image image = Image.FromStream(imageMemStream))
                {
                    Image imagePrint = image;
                    string newImageString = imageString;
                    if (image.Width > maxWidth)
                    {
                        imagePrint = resizeImage(image, maxWidth);
                        using (MemoryStream stream = new MemoryStream())
                        {
                            imagePrint.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                            newImageString = Convert.ToBase64String(stream.ToArray());
                            stream.Close();
                        }
                    }
                    XPathNavigator imgAttr = imgCopy.Clone();
                    bool b = imgAttr.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance");
                    imgAttr.SetValue("false");
                    imgCopy.SetValue(newImageString);
                }
            }

            private static Image resizeImage(Image imgToResize, int newWidth)
            {
                int sourceWidth = imgToResize.Width;
                int sourceHeight = imgToResize.Height;

                float nPercent = 0;
                nPercent = ((float)newWidth / (float)sourceWidth);
                int destWidth = (int)(sourceWidth * nPercent);
                int destHeight = (int)(sourceHeight * nPercent);

                Bitmap b = new Bitmap(destWidth, destHeight);
                Graphics g = Graphics.FromImage((Image)b);
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
                g.Dispose();

                return (Image)b;
            }

Page 1 of 1 (3 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.