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;
}