내 응용 프로그램 (migradoc)에서 PDF 내보내기가 있습니다. GUI가 멈추지 않게하려면 별도의 스레드로 내보내기를 실행하고 싶습니다. PDF에는 Charts도 포함되어 있습니다. 이러한 차트를 내 응용 프로그램에서 사용하는 것처럼 보이게 만들려면 코드에서 만들고 렌더링하십시오. (visifire) 내 스레드가 이미 STA이지만, 내가 WPF 렌더링 명령을 실행하는 예외 얻을 : 다른 스레드가 그것을다른 스레드에서 렌더링 제어
내 코드를 소유하고 있기 때문에 호출 스레드가이 개체에 액세스 할 수 없습니다
를 :
chart.Measure(new Size(311, 180)); chart.Arrange(new Rect(0, 0, 311, 180)); chart.UpdateLayout(); ExportToPng(new Uri("C:\\" + i + "c.png"), chart); public void ExportToPng(Uri path, Chart surface) { if (path == null) return; // Save current canvas transform Transform transform = surface.LayoutTransform; // reset current transform (in case it is scaled or rotated) surface.LayoutTransform = null; // Create a render bitmap and push the surface to it var renderBitmap = new RenderTargetBitmap( (int) surface.Width, (int) surface.Height, 96d, 96d, PixelFormats.Pbgra32); renderBitmap.Render(surface); // Create a file stream for saving image using (var outStream = new FileStream(path.LocalPath, FileMode.Create)) { // Use png encoder for our data var encoder = new PngBitmapEncoder(); // push the rendered bitmap to it encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); // save the data to the stream encoder.Save(outStream); } // Restore previously saved layout surface.LayoutTransform = transform; }
이미이 명령을 발송하려고 시도했지만 여전히 동일한 오류가 발생합니다.
DispatcherHelper.UIDispatcher.BeginInvoke((Action)(() => { chart.Measure(new Size(311, 180)); chart.Arrange(new Rect(0, 0, 311, 180)); chart.UpdateLayout(); ExportToPng(new Uri("C:\\" + i + "c.png"), chart); }));
AFAIK는 GUI 스레드에서만 GUI를 렌더링 할 수 있으며 다른 스레드에서는 렌더링 할 수 없습니다. –
나는 그것을 이미 보내려고했지만, 여전히 같은 오류가 발생한다. (편집 참조) – TheJoeIaut
UI 스레드가 차트 객체를 생성합니까? WPF 개체는 해당 개체가 만들어진 동일한 스레드에서만 업데이트 할 수 있습니다. 따라서 다른 스레드에서 차트를 만든 경우 해당 차트 개체에 대한 모든 업데이트가 해당 특정 스레드에 있어야합니다. – Rachel