画像に文字を埋め込むために、一度画像をCanvasに描画し、文字をCanvas上で追加したあと、以下のコードでCanvas上に描画したものをjpgファイルに変換して保存しているのですが、保存時にコード内のlabelTextの部分だけがトリミングされる場合があります。
きちんとCanvas全体をjpgにして保存したいのですが、どうしたらいいでしょうか?
//追加する文字を設定 TextBox labelText = new TextBox(); labelText.Text = "画像内に埋め込む文字"; //加工する画像をImageに設定 Image fixImage = new Image(); fixImage.Source = bitmapImage; //画像サイズからキャンバスサイズを調整 tempCanvas.Width = bitmapImage.PixelWidth; tempCanvas.Height = bitmapImage.PixelHeight; //画像をキャンバスに追加 tempCanvas.Children.Add(fixImage); //画像に埋め込む文字をキャンバスに追加 Canvas.SetLeft(labelText, 40); Canvas.SetTop(labelText, 40); tempCanvas.Children.Add(labelText); //キャンバスをUIElementに変換 UIElement panel = tempCanvas as UIElement; //画像の保存先フォルダを指定 StorageFolder keyFolder= ApplicationData.Current.LocalFolder; //保存先ファイル名を指定 StorageFile fixFile = await keyFolder.CreateFileAsync("保存するファイル名" + ".jpg", Windows.Storage.CreationCollisionOption.ReplaceExisting); //画像をjpgに変換 // レンダリングをして画像バッファを取得 RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(); await renderTargetBitmap.RenderAsync(panel); var pixelBuffer = await renderTargetBitmap.GetPixelsAsync(); // Encode the image to the selected file on disk using (var fileStream = await fixFile.OpenAsync(FileAccessMode.ReadWrite)) { // JPG形式で保存 var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, fileStream); encoder.SetPixelData( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray()); await encoder.FlushAsync(); }
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。