|
Did you check out the two posts? They contain sample code for getting this to work. I'm sure it's something very simple since you are 98% there.
I'll try and write a sample, ( Karl does VB.NET but will be simple to translate or read ) tonight and post it.
modified 27-Feb-21 21:01pm.
|
|
|
|
|
If you step through the code, what do you see at the line bm.StreamSource = ms ? Before you allocate the source, try setting ms.Position = 0; Alternatively, you can use ms.Seek(0, SeekOrigin.Begin);
-- Edit. The behaviour you are seeing is because the position of the pointer in the MemoryStream is pointing to the end when you have loaded it. You need to reset it back to the start before you can use it.
modified on Wednesday, April 23, 2008 5:30 PM
|
|
|
|
|
Hi,
Pete:
I have tried as you suggested but still no joy. No doubt I'm doing something really stupid so I'll post the code that I use.
This is the code I use to get my list of BitmapImages:
private void Window_Loaded(object sender, RoutedEventArgs e) {
List<BitmapImage> images = GetProductImages();
lstProductImages.ItemsSource = images;
}
private List<BitmapImage> GetProductImages() {
List<BitmapImage> images = new List<BitmapImage>();
using (SqlConnection cn = new SqlConnection(cnString)) {
string sql = "SELECT Top 10 ProductImage From Products WHERE NOT (ProductImage IS NULL)";
SqlCommand cmd = new SqlCommand(sql);
cmd.Connection = cn;
cmd.Connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) {
byte[] productImage = null;
productImage = (byte[])reader["ProductImage"];
using (MemoryStream ms = new MemoryStream(productImage)) {
//ms.Seek(0, SeekOrigin.Begin);
ms.Position = 0;
BitmapImage bm = new BitmapImage();
bm.BeginInit();
//bm.CacheOption = BitmapCacheOption.OnLoad;
bm.StreamSource = ms;
bm.EndInit();
ms.Close();
images.Add(bm);
}
}
cmd.Connection.Close();
}
return images;
}
This is the XAML for the listbox:
<Window.Resources>
<Style TargetType="{x:Type ListBox}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="4" CornerRadius="5" Margin="6">
<!--<Image
Source="{Binding Path=UriSource}"
Stretch="Fill"
Width="100" Height="120"/>-->
<Image
Source="{Binding Path=StreamSource}"
Stretch="Fill"
Width="100" Height="120"/>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter
Property="ScrollViewer.HorizontalScrollBarVisibility"
Value="Disabled"
/>
</Style>
</Window.Resources>
<Grid>
<ListBox Name="lstProductImages"/>
</Grid>
Does anything obvious jump out at you?
Karl:
I've looked at both of those sites and played around with what they suggested to no avail.
Thanks very much,
dlarkin77
|
|
|
|
|
Hi,
I need compelete defination about WPF. Kindly give reference to download said information.
Thank you
(Riaz)
|
|
|
|
|
|
|
How to set mouse position? Something opposite to Mouse.GetPosition(container)
Thanks for help
|
|
|
|
|
Off the top of my head (and without firing up Visual Studio to check) I believe you would use Cursor.Position = new Point(x, y); .
|
|
|
|
|
So fire up VS and see that ur answer is silly
|
|
|
|
|
Yoyosch wrote: So fire up VS and see that ur answer is silly
Nope. I just ran it and it worked fine for me. Have you tried it?
|
|
|
|
|
Of course. This works in WinForms but not in WPF
|
|
|
|
|
There's nothing stopping you adding the WinForms reference to WPF and doing this.
|
|
|
|
|
Yoyosch,
Pete is trying to help you. Telling a Code Project MVP that his answer is silly does not promote good lines of communication. Thanks for working with us as we try and answer questions here.
Have a nice day.
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Sorry I missed you at the MVP summit last week. I did, however, manage to speak to a few people that know you and you received some very high praise indeed.
|
|
|
|
|
Colin,
Me too. Don't know how we missed hooking up. Guess I'll just have to come and visit or get you to come to Charlotte!
Was very nice to meet so many cool and very smart developers. Super week for everyone.
Best to you always!
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Hi there,
I have a stackpanel which contains another stackpanel in which I can browse with a previous and next button. The inside stackpanel contains images . I would like that there is no end in this stack. I mean that the last picture and the first one are joinded.
Thanks in advance,
Globosky
|
|
|
|
|
You are referring to a carousel. Infragistics has one and there are others you can dowload and learn from. You can use their's or write you own.
Check out the recent WPF Panel and WPF 3D Panel artciles here on Code Project.
modified 27-Feb-21 21:01pm.
|
|
|
|
|
Is it possible to use Ajax in WPF Browser Application ?
Best Regards
-----------------
Abhijit Jana
Microsoft Certified Professional
"Success is Journey it's not a destination"
|
|
|
|
|
Yes it is....
All depends on how do you want to make your communications to the server, but you will be enabled to do it ONLY with the server that send you the xbap... that means that if you are trying to run your app from VS2008 and it is configured to open your browser to the file repository you will be not able to test any AJAX, you need to publish it to an IIS and then your AJAX response should be on that same server instance. or at least on that host -no reference to a por number restrictions on the documentation-.
Greetings
Carlos de Luna
|
|
|
|
|
Hi colleagues,
I'm dealing with a weird scenario that I will try to explain here. I don't know what more can I do to find a solution, so I'm posting here with the expectation that someone can help me.
I created a user control using WPF & C# in .NET 3.5 with VS2008. This control is placed in a dll. The control has a toolbar, a tree view on the right side and a browser on the left. Inside the XAML code, I'm inserting the WebBrowser control hosted in a WindowsFormsHost. If I use this control from a Container that uses WPF as well everything works fine.
The problems arise when I try to call the mentioned control from an MFC application. I created a modeless dialog and call my WPF control. The first problem was that WM_CHARS messages were eaten up by someone, and I didn't receive any keystroke in my WPF control. Hopefully, I found this useful link that helped me to solve the problem:
http://devonethatdevelops.wordpress.com/2007/10/30/wpf-win32-interop-with-hwndsource/
But the main problem is that when I put the focus in the web browser and press the TAB key, both the modeless dialog and the application hang. The only thing I can do is go to the task manager and end the process. I've been searching similar problems but found nothing. I've tried to put breakpoints on every KeyDown event both in the containing dialog and in the user control but nothing happens. It seems as though the TAB key absolutely swollen by don't-know-who.
I've executed the WPF control from a WPF container and the KeyDown events reach my control on every key I press, but when I call it from the MFC dialog the program don't stop in the breakpoints.
The only thing I know is that pressing the TAB key inside the web browser control frozens the application.
Please, do you have any idea of what could be happening? Any clue?
Well, thanks anyway for having had the patience of reading this.
Oscar Ortega
|
|
|
|
|
Have you tried KeyboardNavigation.TabNavigation="None" for your user control ?
Sincerely,
-Ron
|
|
|
|
|
Thank you for your answer Ron. The problem is that I'm using the browser to display an InfoPath form, so the user will expect the TAB to be working "as usual" in order to change focus from control to control. So if I tell the user that he's going to lose the TAB key... well, maybe he's not going to be very happy.
But thanks, it's a good a idea if everything else fails to work.
Oscar
|
|
|
|
|
Anyway, Ron, I've just tested the solution you were proposing but no luck: it still happens the same. Application hangs. It seems as though the problem is more on the MFC side than on the WPF side, but it's only a guess...
Thanks,
Oscar
|
|
|
|
|
It was just a guess. There are quite a few options for that setting and it is not really clear how each one behaves. Probably experimenting with them is the only way to find out for sure.
I can see your problem is complex. You might try an MFC forum as well. I'm fresh out of ideas.
Good luck.
Sincerely,
-Ron
|
|
|
|
|
Hello again,
I've reproduced the problem in a small program. First, I've built a WPF Control Library with a WebBrowser embedded. Then, I've built a simple MFC application that calls this control. The result is the same: Whenever I press the TAB key inside the WebBrowser the application hangs and stops responding.
Next, I'm posting the code of this simple project. Please, could someone give me any clue about could be happening?
The WPF control library contains the following code:
In the XAML file:
<code>
<UserControl x:Class="WpfControlLibrary1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wfh="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Loaded="UserControl_Loaded"
Height="300" Width="300">
<Grid>
<wfh:WindowsFormsHost x:Name="myFormHost">
<wf:WebBrowser x:Name="webBrowser1" x:FieldModifier="public" />
<wfh:WindowsFormsHost>
<!-- Uncomment for WPF frame test
<Frame Name="myFrame" Source="http://www.google.es" LoadCompleted="Frame_LoadCompleted" />
-->
<Grid>
</UserControl>
</code>
Then in the .cs file:
<code>
namespace WpfControlLibrary1
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
string destUrl = "http://www.google.es";
webBrowser1.Url = new Uri(destUrl);
}
/* Uncomment for WPF frame test
private void Frame_LoadCompleted(object sender, NavigationEventArgs e)
{
}
*/
}
}
</code>
For the application holding the WPF control, I've built a simple MFC application in which I added a class compiled with the /clr option to manage the transition between MFC (unmanaged) and WPF (managed). Following these lines is the code of this class:
<code>
{...}
#include <vcclr.h>
using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;
using namespace System::Windows::Interop;
using namespace WpfControlLibrary1;
ref class Globals
{
public:
static System::Windows::Interop::HwndSource^ gHwndSource;
static UserControl1^ gwcContainer;
};
HWND hwndWPF; //The hwnd associated with the hosted WPF page
HWND GetHwnd(HWND parent, int x, int y, int width, int height)
{
System::Windows::Interop::HwndSourceParameters^ sourceParams = gcnew System::Windows::Interop::HwndSourceParameters ("MFCWPFApp");
sourceParams->PositionX = x;
sourceParams->PositionY = y;
sourceParams->Height = height;
sourceParams->Width = width;
sourceParams->ParentWindow = IntPtr(parent);
sourceParams->WindowStyle = WS_VISIBLE | WS_CHILD;
Globals::gHwndSource = gcnew System::Windows::Interop::HwndSource(*sourceParams);
Globals::gwcContainer = gcnew UserControl1();
FrameworkElement^ myPage = Globals::gwcContainer;
Globals::gHwndSource->RootVisual = myPage;
return (HWND) Globals::gHwndSource->Handle.ToPointer();
}
/// <summary>
/// Hooks to the HwndSource window procedure. This is required to handle a bug in the WPF interop
/// in case the HwndSource is used in a modal dialog. If the hook is not used, WM_CHAR messages
/// are lost because the standard dialog message loop includes a call to IsDialogMessage().
/// </summary>
static IntPtr ChildHwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, bool% handled)
{
if (msg == WM_GETDLGCODE)
{
handled = true;
//DLGC_WANTARROWS|DLGC_WANTTAB|DLGC_WANTALLKEYS|DLGC_RADIOBUTTON|DLGC_WANTCHARS
return IntPtr(DLGC_WANTCHARS);
}
return IntPtr(0);
}
// CWPFModelessContainerDlg dialog
IMPLEMENT_DYNAMIC(CWPFModelessContainerDlg, CDialog)
CWPFModelessContainerDlg::CWPFModelessContainerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWPFModelessContainerDlg::IDD, pParent)
{
}
{...}
int CWPFModelessContainerDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
hwndWPF = GetHwnd(this->GetSafeHwnd(), lpCreateStruct->x, lpCreateStruct->y, lpCreateStruct->cx, lpCreateStruct->cy);
// Message hook that ensures WM_CHAR messages are dispatched
// to the managed side. Try removing this line and then writing
// to the WPF TextBox
Globals::gHwndSource->AddHook(gcnew HwndSourceHook(ChildHwndSourceHook));
return 0;
}
{...}
void CWPFModelessContainerDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
FrameworkElement^ page;
page = Globals::gwcContainer;
page->MinHeight = cy;
page->MaxHeight = cy;
page->MinWidth = cx;
page->MaxWidth = cx;
::MoveWindow(hwndWPF,0,0,cx,cy,TRUE);
}
</code>
I would appreciate your comments.
Thanks,
Oscar Ortega
|
|
|
|