저는 이미지의 기능에 번호를 매기는 프로젝트를 진행하고 있습니다. 번호가 매겨진 각 항목은 이미지 브러시를 사용하여 배경에 그려진 jpg (또는 bmp/gif/png)가있는 캔버스 위에 인쇄되는 텍스트 블록 객체입니다. 그래픽을 저장할 때가되면 두 개의 옵션이 필요합니다. 하나는 새로운 jpg (또는 다른 형식)로 저장하는 것이고 하나는 textblock 객체가 렌더링 된 것입니다. 또는 옵션 b는 해당 텍스트 블록의 게재 위치를 별도의 파일 (XML, XAML)에 저장하여 되돌아와 항목을 계속 수정할 수 있도록합니다.캔버스에서 전체 이미지를 캡처하려면 어떻게해야합니까?
다음은 제 코드입니다. 이것은 내가 이미지
private void miSaveImage_Click(object sender, RoutedEventArgs e)
{
ImageBrush bi = (ImageBrush)canvas.Background;
Rect bounds = VisualTreeHelper.GetDescendantBounds(canvas);
double pw = (bi.ImageSource as BitmapSource).PixelWidth;
double ph = (bi.ImageSource as BitmapSource).PixelHeight;
double px = (bi.ImageSource as BitmapSource).DpiX;
double py = (bi.ImageSource as BitmapSource).DpiY;
//Get the actual width and height of the image, then the dpi and render an image of it
RenderTargetBitmap rtb = new RenderTargetBitmap((int)pw, (int)ph, px*96, py*96, System.Windows.Media.PixelFormats.Default);
rtb.Render(canvas);
//create the encoder and save the image data to it.
BitmapEncoder pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
try
{
//eventually I need to make this a save dialog but that will come later.
System.IO.MemoryStream ms = new System.IO.MemoryStream();
pngEncoder.Save(ms);
ms.Close();
System.IO.File.WriteAllBytes("Sample.png", ms.ToArray());
}
catch (Exception err)
{
MessageBox.Show(err.ToString(), "Problem saving image", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
이를 저장하는 것을 시도하고있는 곳입니다
는 나는 이미지에 텍스트를 작성하는 데 사용하는 코드 나 이미지 여기
//Global members
Image i;
static Size _Size = new Size();
private void miLoadImage_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".png";
dlg.Filter = @"All supported graphics|*.jpg;*.jpeg;*.png;*.gif;*.bmp|
JPEG Files (*.jpeg)|*.jpeg|
PNG Files (*.png)|*.png|
JPG Files (*.jpg)|*.jpg|
GIF Files (*.gif)|*.gif|
BMP Files (*.bmp)|*.bmp";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
string filename = dlg.FileName;
Uri uri = new Uri(@dlg.FileName, UriKind.Relative);
//Use canvas rather than image
ImageBrush ib = new ImageBrush();
ib.ImageSource = new BitmapImage(new Uri(filename, UriKind.Relative));
canvas.Background = ib;
if (i == null)
i = new Image();
i.Source = new BitmapImage(new Uri(dlg.FileName));
_Size = new Size(i.Source.Width, i.Source.Height);
}
}
를로드하는 데 사용하는 코드입니다.
public void WriteTextToImage(Point position)
{
SolidColorBrush brush = new SolidColorBrush((Color)cpColor.SelectedColor);
//Get something to write on (not an old receipt...)
TextBlock textBlock = new TextBlock();
//Say something useful... well something atleast...
textBlock.Text = tbCurrentLabel.Text;
textBlock.FontSize = slFontSize.Value;
textBlock.Foreground = brush;
Canvas.SetLeft(textBlock, position.X);
Canvas.SetTop(textBlock, position.Y);
canvas.Children.Add(textBlock);
//Need to update the canvas, was not seeing children before doing this.
canvas.UpdateLayout();
//Iterate the label text
IterateLabel();
}
은 지금은 단지 캔버스에서 현재 볼 수있는 이미지의 모서리를 얻고, 그 이미지가의 내가 얼마나을 변경할 수 DPI와 _size 값을 드리면, 아래로 잘립니다되고있다 표시되지만 그 이미지에 고정되어 있습니다. (다른 이미지를로드하면 값이 모두 잘못되어 다시 이미지의 일부만 나타납니다.)
누군가가 제 머리를 뺄 수 있다면 에헴) 나는 가장 감사 할 것입니다! TIA
유 답을 확인해야 – AnjumSKhan