set height of icon Image template programatically - c#

I have one column in my datagrid which contains Icons.
for this I have one celltemplate added to column programatically.
var imageFactory = new FrameworkElementFactory(typeof(System.Windows.Controls.Image));
imageFactory.SetBinding(System.Windows.Controls.Image.SourceProperty, imageBinding);
imageFactory.SetValue(System.Windows.Controls.Image.StretchProperty, Stretch.None);
if (config.Font != null)
{
double height = config.Font.Size;
imageFactory.SetValue(FrameworkElement.HeightProperty, height);
}
var dataTemplate = new DataTemplate { VisualTree = imageFactory };
statusColumn.CellTemplate = dataTemplate;
view.DataGrid.Columns.Add(statusColumn);
when I set Height property externally it crops the image instead of resizing image to 'height' value.
how to set image height to specific value.
please suggest.

try this
double size = 14.0;
BitmapImage bmp = new BitmapImage(new Uri("MyIcon.ico", UriKind.RelativeOrAbsolute));
FrameworkElementFactory icon = new FrameworkElementFactory(typeof(Image));
icon.SetValue(Image.SourceProperty, bmp);
icon.SetValue(Image.WidthProperty, size);
icon.SetValue(Image.HeightProperty, size);
UPDATE try this
Style sBase = (Style)this.Resources["BaseButtonStyle"];
Style sNew = new Style(typeof(Image), sBase);
sNew.Setters.Add(new Setter(HeightProperty, 20d));
REFERENCE
See this

I used BitmapImage.DecodePixelHeight and it solved my problem. :)
bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memoryStream;
bitmapImage.DecodePixelHeight = font.Size <= 9 ? font.Size + 2 : font.Size;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();

Related

Printed wpf visual is clipped in landscape mode under Windows 8.1

I have a simple wpf desktop application which prints a bitmap in landscape mode.
Under Windows 8/8.1 the printout is clipped on the bottom of the page while under Windows 7 it is printed correctly.
The code is really simple: load a bitmap, put it into an Image object, measure the printable area, arrange the image and print.
void printButton_Click(object sender, RoutedEventArgs e)
{
var pd = new PrintDialog();
if (!pd.ShowDialog().Value)
{
return;
}
pd.PrintTicket.PageOrientation = PageOrientation.Landscape;
pd.PrintTicket.PageBorderless = PageBorderless.None;
var printingCapabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri("D:\\printTest.bmp");
bitmapImage.EndInit();
var imageuiElement = new Image { Source = bitmapImage };
var desiredSize = new Size(printingCapabilities.PageImageableArea.ExtentWidth, printingCapabilities.PageImageableArea.ExtentHeight);
imageuiElement.Measure(desiredSize);
imageuiElement.Arrange(new Rect(new Point(printingCapabilities.PageImageableArea.OriginWidth, printingCapabilities.PageImageableArea.OriginHeight), imageuiElement.DesiredSize));
pd.PrintVisual(imageuiElement, "MyImage");
}
The bitmap size is 1518 x 1092 pixels, 96 DPI, which is 40.2 x 28.9 cm.
I have found the question Cannot print a document with landscape orientation under Windows 8 (WPF, .NET 4.0)
but there is no good response for my issue (additionally I have no problem with printing as landscape itself).
I have tested it with different printers of different vendors, the printouts are clipped in all of them. A software CutePDF writer prints it to PDF correctly.
Any help appreciated.
It seems that Windows 8 does not draw ui elements outside of their container bounds. This is why the printout was clipped at the bottom.
Anyway, I ended up with a code like this, which is able to print a centered bitmap on a landscape page:
printDialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
printDialog.PrintTicket.PageBorderless = PageBorderless.None;
var printingCapabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
if (printingCapabilities.PageImageableArea == null)
{
return;
}
var document = new FixedDocument();
document.DocumentPaginator.PageSize = new Size(printingCapabilities.PageImageableArea.ExtentWidth, printingCapabilities.PageImageableArea.ExtentHeight);
foreach (var imageStream in imageStreams)
{
document.Pages.Add(GeneratePageContent(imageStream, printingCapabilities, printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight));
}
try
{
printDialog.PrintDocument(document.DocumentPaginator, GlobalConstants.SoftwareName);
}
private PageContent GeneratePageContent(Stream imageStream, PrintCapabilities printingCapabilities, double paperWidth, double paperHeight)
{
imageStream.Seek(0, SeekOrigin.Begin);
var bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = imageStream;
bmp.EndInit();
var margin = new Thickness();
var pageSize = new Size();
if (printingCapabilities.PageImageableArea != null)
{
margin = new Thickness(
printingCapabilities.PageImageableArea.OriginWidth,
printingCapabilities.PageImageableArea.OriginHeight,
printingCapabilities.PageImageableArea.OriginWidth,
printingCapabilities.PageImageableArea.OriginHeight);
pageSize = new Size(printingCapabilities.PageImageableArea.ExtentWidth, printingCapabilities.PageImageableArea.ExtentHeight);
}
var imageUiElement = new Image
{
Source = bmp,
Margin = margin
};
var canvas = new Grid { Width = paperWidth, Height = paperHeight };
canvas.Children.Add(imageUiElement);
var fixedPage = new FixedPage
{
Width = paperWidth,
Height = paperHeight
};
fixedPage.Children.Add(canvas);
var pageContent = new PageContent();
((IAddChild)pageContent).AddChild(fixedPage);
pageContent.Measure(pageSize);
pageContent.Arrange(new Rect(new Point(), pageSize));
pageContent.UpdateLayout();
return pageContent;
}

Load an image from media library onto canvas with WriteableBitmap image?

I am trying to load a picture selected from media library of windows phone and I have done upto choosing the required picture and I am unable to load image onto my canvas named area with this code :
void photochoosertask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
WriteableBitmap bitMap = new WriteableBitmap(200,200);
Extensions.LoadJpeg(bitMap, e.ChosenPhoto);
Canvas.SetLeft(area, 10);
Canvas.SetTop(area, 10);
bitMap.Render(area, null);
bitMap.Invalidate();
}
}
But I am unable to do with this code..any suggestions..??
or how to do this task ? Is this the correct way ?
Thanks
if (e.TaskResult == TaskResult.OK)
{
BitmapImage bi = new BitmapImage();
bi.SetSource(e.ChosenPhoto);
WriteableBitmap b = new WriteableBitmap(bi);
Image img = new Image();
img.Source = b;
Canvas.SetLeft(img, 10);
Canvas.SetTop(img, 10);
area.Children.Add(img);
}
In order to show the bitmap in your Canvas you would have to add an Image control to its Children collection, which uses the bitmap as its Source:
var bitmap = new WriteableBitmap(200, 200);
Extensions.LoadJpeg(bitmap, e.ChosenPhoto);
var image = new Image();
image.Source = bitmap;
Canvas.SetLeft(image, 10);
Canvas.SetTop(image, 10);
area.Children.Add(image);
As e.ChosenPhoto is a stream, you may perhaps also use a BitmapImage instead of a WriteableBitmap, and set its source stream to e.ChosenPhoto. You may then set the size of the Image control to the desired values.
var bitmap = new BitmapImage();
bitmap.SetSource(e.ChosePhoto);
var image = new Image();
image.Source = bitmap;
image.Width = 200;
image.Height = 200;
Canvas.SetLeft(image, 10);
Canvas.SetTop(image, 10);
area.Children.Add(image);

It does not find my image

Image icon = null;
if (/* some logic test that succeeds*/)
{
icon = new Image();
icon.Width = 32;
icon.Height = 32;
icon.Stretch = Stretch.Fill;
icon.Source = new BitmapImage(new Uri("/Images/icons/flower.png", UriKind.Relative)); ;
icon.IsHitTestVisible = false;
}
if (icon != null)
MyCanvas.Children.Add(icon);
I dont know why this fails. I store my image as content in the Images/Icons folder in the project. Still after the I've set the source (.Source above) the PixelHeight and Width is 0 of the source.
Does anyone know why?

Show image above SMFPlayer issue

An image (Image class) is placed above a SMFPlayer (both elements are created in code-behind). Z-index of the image is the Z-Index of SMFPlayer + 1. The image is resized (adjusting the width) according to the playing progress of SMFPlayer.
videoPlayer = new SMFPlayer();
videoPlayer.Width = 1920;
videoPlayer.Height = 1080;
videoPlayer.Margin = new Thickness(1920, 0, 0, 0);
PlaylistItem item = new PlaylistItem();
Random r = new Random();
item.MediaSource = new Uri("video.wmv");
item.DeliveryMethod = DeliveryMethods.ProgressiveDownload;
videoPlayer.Playlist.Add(item);
videoPlayer.AutoPlay = true;
videoPlayer.AutoLoad = true;
videoPlayer.IsControlStripVisible = false;
videoPlayer.PlaylistVisibility = FeatureVisibility.Disabled;
videoPlayer.MediaEnded += new EventHandler(player_MediaEnded);
LayoutRoot.Children.Add(videoPlayer);
bar_yellow3 = new Image();
bar_yellow3.Source = new BitmapImage(new Uri("/SMF_ProgressiveDownload1;component/assets/bar_y.png", UriKind.Relative));
bar_yellow3.Width = 775;
bar_yellow3.Height = 34;
bar_yellow3.Margin = new Thickness(2948,1034,0,0);
bar_yellow3.Stretch = Stretch.Fill;
bar_yellow3.VerticalAlignment = VerticalAlignment.Top;
bar_yellow3.HorizontalAlignment = HorizontalAlignment.Left;
LayoutRoot.Children.Add(bar_yellow3);
However, when the playing progress is less than 20%, the image blinks randomly. When the SMFPlayer is set to be invisible ( Visibility.Collapsed ) , the image is normal.
I have tried to call the update function of the Image, which is: bar_yellow3.UpdateLayout(); but the method does not solve the blinking issue.
Any solution?
Try use effects (Shazzam will help you) instead using Z order.

Generate image from XAML View

I would like to generate some images dynamicaly. For that, I intend to create a XAML View, populate it with Data (using DataBinding) and then generate an image from the rendering of that view (kind of a screenshot).
Is there a way to do this in Silverligth or WPF?
In WPF:
public static Image GetImage(Visual target)
{
if (target == null)
{
return null; // No visual - no image.
}
var bounds = VisualTreeHelper.GetDescendantBounds(target);
var bitmapHeight = 0;
var bitmapWidth = 0;
if (bounds != Rect.Empty)
{
bitmapHeight = (int)(Math.Floor(bounds.Height) + 1);
bitmapWidth = (int)(Math.Floor(bounds.Width) + 1);
}
const double dpi = 96.0;
var renderBitmap =
new RenderTargetBitmap(bitmapWidth, bitmapHeight, dpi, dpi, PixelFormats.Pbgra32);
var visual = new DrawingVisual();
using (var context = visual.RenderOpen())
{
var brush = new VisualBrush(target);
context.DrawRectangle(brush, null, new Rect(new Point(), bounds.Size));
}
renderBitmap.Render(visual);
return new Image
{
Source = renderBitmap,
Width = bitmapWidth,
Height = bitmapHeight
};
}
Use the WriteableBitmap and its Render function in Silverlight.
In WPF use this trick by using the RenderTargetBitmap and its Render function
You can add the controls (data after they are databound) that you want to capture to a ViewBox - http://www.wpftutorial.net/ViewBox.html
From there, you can create an image using WriteableBitmap - http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap%28VS.95%29.aspx

Categories