Share via

Lineargradient not working

Edgar MWANDIRA 0 Reputation points
2023-05-07T08:22:13.7033333+00:00
Hello,

I don't know where I am going wrong. I am trying to apply a Linear gradient to a rectangle in a DocumentPaginator. I have attached full code and XAML. When I select just one color, at least it displays but as solid color (not in lineargradient. When I include the other colors, nothing gets displayed at all.

using System.Windows.Xps;

using System.Windows.Xps.Packaging;

using System.IO;

using System.Threading;

using System.Windows;

using System.Windows.Documents;

using System.Collections.Generic;

using System.Windows.Markup;

using System.Windows.Media;

namespace WpfApp3

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

		FixedDocumentSequence Invoices = new FixedDocumentSequence();

		XpsDocument newXpsDocument;

		List<XpsDocument> XpsDocumentList = new List<XpsDocument>();

		public MainWindow()

        {

            InitializeComponent();

			NewDocumentPaginator paginator = new NewDocumentPaginator();

			//string tempFileName =System.IO.Path.GetTempFileName();

			string tempFileName = @"C:\Users\watfo\OneDrive\Desktop\tax";

			XpsDocument xpsDocument = new XpsDocument(tempFileName, FileAccess.ReadWrite);

			XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);

			writer.Write(paginator);

			XpsDocumentList.Add(xpsDocument);

			this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new ThreadStart(DisplayDocumentViewer));

		}

		public void AddXPSDocument(FixedDocumentSequence xpsOld)

		{

			foreach (DocumentReference r in xpsOld.References)

			{

				DocumentReference newRef = new DocumentReference();

				((IUriContext)newRef).BaseUri = ((IUriContext)r).BaseUri;

				newRef.Source = r.Source;

				Invoices.References.Add(newRef);

			}

		}

		private void DisplayDocumentViewer()

		{

			foreach (XpsDocument XpsDocument in XpsDocumentList)

			{

				FixedDocumentSequence fixedDocumentSequence = XpsDocument.GetFixedDocumentSequence();

				AddXPSDocument(XpsDocument.GetFixedDocumentSequence() as FixedDocumentSequence);

            }

			DocumentViewer1.Document = Invoices as IDocumentPaginatorSource;

		}

	}

}

public class NewDocumentPaginator : DocumentPaginator

{

	public override bool IsPageCountValid

	{

		get { return true; }

	}

	public override int PageCount

	{

		get { return 1; }

	}

	public override Size PageSize

	{

		get { return new Size(96 * 11, 96 * 8.5); }

		set { }

	}

	public override IDocumentPaginatorSource Source

	{

		get { return null; }

	}

	public override DocumentPage GetPage(int pageNumber)

	{

		DrawingVisual visual = new DrawingVisual();

		using (DrawingContext dc = visual.RenderOpen())

		{

			Rect rect = new Rect(10, 10, 300, 30);

			LinearGradientBrush myVerticalGradient = new LinearGradientBrush();

			myVerticalGradient.StartPoint = new Point(0.5, 0);

			myVerticalGradient.EndPoint = new Point(0.5, 1);

			myVerticalGradient.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));

			//myVerticalGradient.GradientStops.Add(new GradientStop(Colors.Red, 0.25));

			//myVerticalGradient.GradientStops.Add(new GradientStop(Colors.Blue, 0.75));

			//myVerticalGradient.GradientStops.Add(new GradientStop(Colors.LimeGreen, 1.0));

            dc.DrawRectangle(myVerticalGradient, (System.Windows.Media.Pen)null, rect);

		}

		return new DocumentPage(visual, PageSize, new Rect(PageSize), new Rect(PageSize));

	}

}

<Window x:Class="WpfApp3.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfApp3"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <DocumentViewer x:Name="DocumentViewer1"/> 

    </Grid>

</Window>


Developer technologies | Windows Presentation Foundation

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.