wpf, BitmapDecoder issue - wpf

In a dialog window's code-behond, I select icon size based on my application's font size the following way:
double iconSize = 24;
if (Session.FontSize <= 16)
iconSize = 48;
else if (Session.FontSize >= 28)
iconSize = 72;
Uri uri = new Uri("pack://application:,,,/MyApp;component/Resources/
Status-dialog-information.ico");
BitmapDecoder decoder = BitmapDecoder.Create(uri,
BitmapCreateOptions.DelayCreation,
BitmapCacheOption.Default);
BitmapSource bms = decoder.Frames.
Where(f => f.Width == iconSize).FirstOrDefault();
imageControl.Source = bms;
, where imageControl is, well, Image control. I use icons from Oxygen set (found in iconarchive.com)
Now, the above code works with some of the icons, with no problem.
But for some other icon files, such as the above, it works when I create my dialog window for the first time only. Second time I creat a dialog window BitmapDecoder.Create() method throws System.IO.FileFormatException - "The image format is unrecognized". Please suggest how to fix it.

Probably difficult to answer, without more details. I just created static field to initialize once.

Related

WPF event within a frame stored on a stackpanel

I need to recreate a program similar to whatsapp that can send and receive messages, images videos and audio. I have created a WPF form to show messages that looks like this:
I have a stack panel that contains text bubbles on them. Text messages work fine but if I send an image I want the user to be able to click on the image text bubble and it must become full screen. The image text bubble consists of a label that has a frame in it and then within that frame the image is stored. The label was used since we could resize the label.
However, because the image is done like this dynamically, we cannot seem to register an on-click event on this image bubble. If you have any better ways that we can display the image or how to log this event it would be much appreciated. Here is the method used to add the image.
public void AddMessage_Image(string path, string displayName, int role, string date = "")
{
//Create an image from the path
ImageBrush image = new ImageBrush();
image.ImageSource = new BitmapImage(new Uri(path, UriKind.Absolute));
image.Stretch = Stretch.Uniform;
//Create a frame in which to place the image
Frame fr = new Frame();
fr.Background = image;
fr.MinHeight = 120;
fr.MinWidth = 160;
//Ensure scalabilty of the image
Viewbox vb = new Viewbox();
vb.Child = fr;
vb.Stretch = Stretch.Uniform;
//Place the image in a sizable container
Label lbl = new Label();
lbl.MinHeight = 10;
lbl.MinWidth = 10;
lbl.MaxHeight = 300;
lbl.MaxWidth = 400;
lbl.Content = vb;
if (role == (int) Role.Sender)
lbl.HorizontalAlignment = HorizontalAlignment.Right;
else
lbl.HorizontalAlignment = HorizontalAlignment.Left;
lbl.Background = Brushes.Black;
//Place the image in the chat
chatbox.Children.Add(lbl);
}

How to set ImageView to show different position in PNG? (Android sprite alike animations)

I have the following png:
Each Icon is 100X100 px. All in all 800X100 px.
I have the following ImageView xml:
<ImageView
android:id="#+id/CycleStageImage"
android:layout_width="100dp"
android:layout_height="100dp">
I would like to set CycleStageImage to be show different Icon (100,100) on Timer Interval of 1 Second back and forth.
I am having a problem generating a code that moves on Axis of this PNG.
I have tried the followings from multiple links over SOF, but with no luck:
//first try - not working
//Resources res = mainActivity.ApplicationContext.Resources;
//Bitmap bitmap = BitmapFactory.DecodeResource(res, Resource.Id.CycleImage);
//BitmapDrawable bitmapDrawable = new BitmapDrawable(Resources.System, bitmap);
//ClipDrawable clipDrawable = new ClipDrawable(bitmapDrawable, GravityFlags.Center, ClipDrawable.Horizontal);
//clipDrawable.SetBounds(100, 100, 100, 100);
//clipDrawable.SetLevel(100);
//imageView.SetImageResource(Android.Resource.Color.Transparent);
//imageView.SetImageDrawable(clipDrawable);
//second try - shows only part of the left top corner
//double TUNNING = 0.5; //0.5 cut in half
//Bitmap srcBmp = BitmapFactory.DecodeResource(Resources.System, cycleStage);
//Bitmap modBmp = Bitmap.CreateBitmap(
// srcBmp,
// 0,
// srcBmp.Height, // TUNNING
// srcBmp.Height,
// srcBmp.Height
// );
//third try - same as the second try.
//int START_X = 0;
//int START_Y = 100;
//int WIDTH_PX = 100;
//int HEIGHT_PX = 100;
//// Crop bitmap
//Bitmap newBitmap = Bitmap.CreateBitmap(SOURCE_BITMAP, START_X, START_Y, WIDTH_PX, HEIGHT_PX, null, false);
//// Assign new bitmap to ImageView
//imageView.SetImageBitmap(newBitmap);
I have followed the Android tutorial:
https://developer.android.com/guide/topics/resources/drawable-resource.html#Clip
but with no luck..
It would be much appreciated the help with timer as well with the png.
Thanks!
Finally I got it to work, following these steps:
(Although I admit its a bit lame...)
I divided the above PNG into 8 (100x100) PNGs each using online tool -> PNG Splitter
I created a GIF using online tool - GIF Maker -> http://gifmaker.me/
I have placed the saved gif under "Assets" folder and changed its properties-> build action to "AndroidAsset"
I have created WebView and placed it inside my XML screen. it looks like this:
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyWebView"
android:layout_width="100dp"
android:layout_height="100dp"/>
In my Activity I generated the code as follows:
WebView myWebView = (WebView)findViewById(Resource.Id.MyWebView);
myWebView.loadUrl("file:///android_asset/Gif.gif");
It works like a charm!
I hope it will be of assistant to anyone who desires to accomplish running GIF's inside android application.
Good Luck.

dynamically added control not rendering, silverlight

Hello i have a canvas with the name Layout. I want to add some childs to it in code behind. Here is the code.
private void AddItem(int TruePosition, int CurrentPosition, string ImageFileName)
{
Image img = new Image();
img.Source = new BitmapImage(new Uri(#"/Images/" + ImageFileName, UriKind.Relative));
img.Width = 100.0;
img.Height = 400.0;
img.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
img.Stretch = Stretch.None;
Border b = new Border();
b.SetValue(Canvas.TopProperty, 200.0);
b.SetValue(Canvas.LeftProperty, (double)CurrentPosition);
b.SetValue(Canvas.ZIndexProperty, 1);
b.Background = new SolidColorBrush(Colors.Blue);
b.MouseMove += new MouseEventHandler(Border_MouseMove);
b.MouseLeftButtonDown += new MouseButtonEventHandler(Border_MouseLeftButtonDown);
b.MouseLeftButtonUp += new MouseButtonEventHandler(Border_MouseLeftButtonUp);
b.Child = img;
Layout.Children.Add(b);
UpdateLayout();
}
The image Uri is probably good because if i try to add only the border(without the image) it's still not rendering. Also i call this AddItem function from a button click event handler so initializations should not be a problem. Probably i am missing a very basic stuff here. Any ideas are welcome, thank you for your help.
Update: Ok i had something wrong with the browser cache probably, the border is added, but the image in the border isn't showing up. In the Visual Studio project i have an Images dir with the image files. Am i setting the path to them wrong?
So, now i am allowed to answer it. Well it was a newbie mistake.
The first / before Images is not needed. new Uri(#"Images/" + ImageFileName, UriKind.Relative) is correct.

Silverlight specific Image shifting to the right

I am generating a set images to form a human body so that I can use for a physics engine.
The images generated are in a specific user control in where I set the dimentions and co-ordinates of each image. That usercontrol is then loaded in another user control but for some reason when the images are loaded, one specific image which I named (rightBicep) is shifting to the right. Here is a screenshot :
alt text http://img193.imageshack.us/img193/592/imageshift.jpg
I illustrated the positions of the images with dotted lines, the green dotted line is refering to where the image should be located, and the red dotted line is where the image is being shown.
The weird thing is the image beneath it (called rightForearm) take's it's LeftPosition from it, and when during debugging they have the exact same leftProperty value. Here's the syntax :
public void generateRightBicep(string imageUrl)
{
rightBicep = new Image();
rightBicep.Name = CharacterName + "rightbicep";
Uri imageUri = new Uri(imageUrl, UriKind.Relative);
LayoutRoot.Children.Add(rightBicep);
rightBicep.Source = new BitmapImage(imageUri);
rightBicep.ImageOpened += new EventHandler<RoutedEventArgs>(bodyPart_ImageOpened);
}
public void rightBicepLoaded()
{
var bi = waitTillImageLoad(rightBicep.Name);
rightBicep.Height = elbowToArmpit + (2 * palm);
rightBicep.Width = ratio(bi.PixelHeight, bi.PixelHeight, rightBicep.Height); // to be determined
Vector2 topVector;
topVector.X = (float)(Convert.ToDouble(torso.GetValue(Canvas.LeftProperty)) - palm);
topVector.Y = (float)(Convert.ToDouble(neck.GetValue(Canvas.TopProperty)) + neck.Height);
if (!faceRight)
{
perspectiveVectorHeight(ref topVector, ref rightBicep, torso.Width);
rightBicep.Width = ratio(bi.PixelHeight, bi.PixelHeight, rightBicep.Height);
}
rightBicep.SetValue(Canvas.LeftProperty, Convert.ToDouble(topVector.X));
rightBicep.SetValue(Canvas.TopProperty, Convert.ToDouble(topVector.Y));
rightBicep.SetValue(Canvas.ZIndexProperty, rightBicepZindex);
generateRightShoulder();
}
public void generateRightForearm(string imageUrl)
{
rightForearm = new Image();
rightForearm.Name = CharacterName + "rightforearm";
Uri imageUri = new Uri(imageUrl, UriKind.Relative);
LayoutRoot.Children.Add(rightForearm);
rightForearm.Source = new BitmapImage(imageUri);
rightForearm.ImageOpened += new EventHandler<RoutedEventArgs>(bodyPart_ImageOpened);
}
public void rightForearmLoaded()
{
var bi = waitTillImageLoad(rightForearm.Name);
rightForearm.Height = (elbowToHandTip - handLength) + palm;
rightForearm.Width = ratio(bi.PixelHeight, bi.PixelWidth, rightForearm.Height);
Vector2 topVector;
if (faceRight)
{
topVector.X = (float)(Convert.ToDouble(rightBicep.GetValue(Canvas.LeftProperty)));
topVector.Y = (float)(Convert.ToDouble(rightBicep.GetValue(Canvas.TopProperty)) + rightBicep.Height - palm);
}
else
{
topVector.X = (float)(Convert.ToDouble(leftBicep.GetValue(Canvas.LeftProperty)));
topVector.Y = (float)(Convert.ToDouble(leftBicep.GetValue(Canvas.TopProperty)) + leftBicep.Height - palm);
perspectiveVectorHeight(ref topVector, ref rightForearm, torso.Width);
rightForearm.Width = ratio(bi.PixelHeight, bi.PixelWidth, rightForearm.Height);
}
rightForearm.SetValue(Canvas.LeftProperty, Convert.ToDouble(topVector.X));
rightForearm.SetValue(Canvas.TopProperty, Convert.ToDouble(topVector.Y));
rightForearm.SetValue(Canvas.ZIndexProperty, rightForearmZIndex);
generateRightElbow();
}
Now all the values I am adding together are a group of doubles I preset, and the property faceRight is to dertmine if the human body is facing right or left to determine where the positions of the body parts (since if the right hand looks on the left hand side when the human body turns the other way).
If you notice the rightforearm is taking the leftproperty of the rightbicep, so technically it should display direcrly underneath which it isn't. I also debugged the user control and both have the left property of -3.
PS. I call the methods rightbicepLoaded and rightforearmLoaded when an event is called when all the imageOpened events all have been triggered.
Any ideas on why this is happening?
Found out why , in my method ratio it should take hieght and width, and I put and i put 2 hieghts instead

Printing a WPF FlowDocument

I'm building a demo app in WPF, which is new to me. I'm currently displaying text in a FlowDocument, and need to print it.
The code I'm using looks like this:
PrintDialog pd = new PrintDialog();
fd.PageHeight = pd.PrintableAreaHeight;
fd.PageWidth = pd.PrintableAreaWidth;
fd.PagePadding = new Thickness(50);
fd.ColumnGap = 0;
fd.ColumnWidth = pd.PrintableAreaWidth;
IDocumentPaginatorSource dps = fd;
pd.PrintDocument(dps.DocumentPaginator, "flow doc");
fd is my FlowDocument, and for now I'm using the default printer instead of allowing the user to specify print options. It works OK, except that after the document prints, the FlowDocument displayed on screen has changed to to use the settings I specified for printing.
I can fix this by manually resetting everything after I print, but is this the best way? Should I make a copy of the FlowDocument before I print it? Or is there another approach I should consider?
yes, make a copy of the FlowDocument before printing it. This is because the pagination and margins will be different. This works for me.
private void DoThePrint(System.Windows.Documents.FlowDocument document)
{
// Clone the source document's content into a new FlowDocument.
// This is because the pagination for the printer needs to be
// done differently than the pagination for the displayed page.
// We print the copy, rather that the original FlowDocument.
System.IO.MemoryStream s = new System.IO.MemoryStream();
TextRange source = new TextRange(document.ContentStart, document.ContentEnd);
source.Save(s, DataFormats.Xaml);
FlowDocument copy = new FlowDocument();
TextRange dest = new TextRange(copy.ContentStart, copy.ContentEnd);
dest.Load(s, DataFormats.Xaml);
// Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog,
// and allowing the user to select a printer.
// get information about the dimensions of the seleted printer+media.
System.Printing.PrintDocumentImageableArea ia = null;
System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(ref ia);
if (docWriter != null && ia != null)
{
DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator;
// Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device.
paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight);
Thickness t = new Thickness(72); // copy.PagePadding;
copy.PagePadding = new Thickness(
Math.Max(ia.OriginWidth, t.Left),
Math.Max(ia.OriginHeight, t.Top),
Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right),
Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom));
copy.ColumnWidth = double.PositiveInfinity;
//copy.PageWidth = 528; // allow the page to be the natural with of the output device
// Send content to the printer.
docWriter.Write(paginator);
}
}
You can use the code from the URL below, it wraps the flow document in a fixed document and prints that, the big advantage is that you can use it to add margin, headers and footers.
https://web.archive.org/web/20150502085246/http://blogs.msdn.com:80/b/fyuan/archive/2007/03/10/convert-xaml-flow-document-to-xps-with-style-multiple-page-page-size-header-margin.aspx
The following works with both text and non-text visuals:
//Clone the source document
var str = XamlWriter.Save(FlowDoc);
var stringReader = new System.IO.StringReader(str);
var xmlReader = XmlReader.Create(stringReader);
var CloneDoc = XamlReader.Load(xmlReader) as FlowDocument;
//Now print using PrintDialog
var pd = new PrintDialog();
if (pd.ShowDialog().Value)
{
CloneDoc.PageHeight = pd.PrintableAreaHeight;
CloneDoc.PageWidth = pd.PrintableAreaWidth;
IDocumentPaginatorSource idocument = CloneDoc as IDocumentPaginatorSource;
pd.PrintDocument(idocument.DocumentPaginator, "Printing FlowDocument");
}
I am also generating a WPF report off a Flow document, but I am purposely using the flow document as a print preview screen. I there for want the margins to be the same. You can read about how I did this here.
In your scenario I'm thinking why not just make a copy of your settings, instead of the entire flow document. You can then re-apply the settings if you wish to return the document back to it's original state.

Resources