Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
There are some rows in my dataGrid, when i select one of them and print it's data, only first image of database will print while i only need to print selected row image.

XML
<Image VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="Fill" Name="PictureBox"
                       Source="{Binding Picture}" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}" Opacity="2">
	</Image>


C#
private void Print_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog();
            if (printDialog.ShowDialog() == true)
            {
                
                DrawingVisual dv = new DrawingVisual();
                var dc = dv.RenderOpen();
              
                SqlConnection con = new SqlConnection();
                con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Database\Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                SqlCommand cmd = new SqlCommand();
                BitmapImage bmp = new BitmapImage();
                cmd.CommandText = "SELECT Picture FROM DataBase";
                cmd.Connection = con;
                con.Open();
                bmp.CacheOption = BitmapCacheOption.OnLoad;
                bmp.BeginInit();
                bmp.StreamSource = new System.IO.MemoryStream((Byte[])cmd.ExecuteScalar());
                bmp.EndInit();
                dc.DrawImage(bmp, new Rect(140, 170, 150, 150));
-

                dc.DrawText(new FormattedText("Name:", CultureInfo.GetCultureInfo("en-us"), FlowDirection,
                     new Typeface(new System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Bold,
                         FontStretches.Normal), 12, System.Windows.Media.Brushes.Black), new System.Windows.Point(700, 180));
                dc.DrawText(new FormattedText(txtName.Text, CultureInfo.GetCultureInfo("en-us"), FlowDirection,
                      new Typeface(new System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Normal,
                          FontStretches.Normal), 11, System.Windows.Media.Brushes.Black), new System.Windows.Point(550, 180));

                dc.Close();             
                printDialog.PrintVisual(dv, "Print");
            }
		}
Posted
Updated 15-Aug-12 6:01am
v2
Comments
[no name] 15-Aug-12 12:04pm    
You are not selecting your image based on any kind of identifier.
M.H. Shojaei 15-Aug-12 12:07pm    
What type of identifier?
[no name] 15-Aug-12 12:12pm    
Any kind. Name, number, index. Whatever you are using in your database.
M.H. Shojaei 15-Aug-12 12:15pm    
If I use Name or ID, have you guidance or link?
[no name] 15-Aug-12 12:17pm    
http://www.w3schools.com/sql/sql_where.asp

1 solution

I solved it.

C#
private void Print_Click(object sender, RoutedEventArgs e)
{
    System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog();
    if (printDialog.ShowDialog() == true)
    {                
        DrawingVisual dv = new DrawingVisual();
        var dc = dv.RenderOpen();

         SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Database\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            SqlCommand cmd = new SqlCommand();

            BitmapImage bmp = new BitmapImage();
            cmd.CommandText = "Select Picture from Database where Code=@Code";
            cmd.Parameters.Add("@Code", SqlDbType.NVarChar, 30);
            cmd.Parameters["@Code"].Value = this.txtCode.Text;
            cmd.Connection = con;
            con.Open();

            bmp.CacheOption = BitmapCacheOption.OnLoad;
            bmp.BeginInit();
            bmp.StreamSource = new System.IO.MemoryStream((Byte[])cmd.ExecuteScalar());
            bmp.EndInit();
            dc.DrawImage(bmp, new Rect(140, 170, 150, 150));

        dc.DrawText(new FormattedText("Name:", CultureInfo.GetCultureInfo("en-us"), FlowDirection,
             new Typeface(new System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Bold,
                 FontStretches.Normal), 12, System.Windows.Media.Brushes.Black), new System.Windows.Point(700, 180));
        dc.DrawText(new FormattedText(txtName.Text, CultureInfo.GetCultureInfo("en-us"), FlowDirection,
              new Typeface(new System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Normal,
                  FontStretches.Normal), 11, System.Windows.Media.Brushes.Black), new System.Windows.Point(550, 180));


        dc.Close();

        printDialog.PrintVisual(dv, "Print");
    }
 
Share this answer
 
Comments
ArianDiba 19-Aug-12 11:16am    
This was good snippet for me,
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900