在 WPF 中加载并合并两个图像

发布于 2024-11-07 09:26:58 字数 853 浏览 0 评论 0原文

我尝试编写一个应用程序,从 WPF 中加载的 jpeg 创建带水印的图像。 我想在 WPF 中加载 jpeg 图像,并使用带有透明区域的预定义 png 来绘制它。 我尝试创建两个图像作为 RenderTargetBitmap,然后创建一个新的 RenderTargetBitmap,

Image LoadSource(string file) {
  var image = new Image();
  var src = new BitmapImage(); 
  src.BeginInit(); 
  src.UriSource = new Uri(file, UriKind.Absolute);
  src.CacheOption = BitmapCacheOption.OnLoad;
  src.EndInit();
  image.Source = src;
  image.Stretch = Stretch.Uniform;
  image.Width = 1024;
  image.Height = 768;
  return image;
}

private void Window_Loaded(object sender, RoutedEventArgs e) {
  var imgA = LoadSource(@"D:\test\1.jpg");
  var imgB = LoadSource(@"D:\test\2.png");
  var bmp = new RenderTargetBitmap(1024, 768, 120, 96, PixelFormats.Pbgra32);
  bmp.Render(imgA);
  bmp.Render(imgB);
  ResultImage.Source = bmp;
}

但它不起作用。

谁能指出我的解决方案吗?

I try to write an application that create a watermarked image from the loaded jpeg in WPF.
I want to load in WPF an jpeg image and draw it with a predifined png with a transparence region over.
I've tried to create two images as the RenderTargetBitmap and then create a new RenderTargetBitmap like

Image LoadSource(string file) {
  var image = new Image();
  var src = new BitmapImage(); 
  src.BeginInit(); 
  src.UriSource = new Uri(file, UriKind.Absolute);
  src.CacheOption = BitmapCacheOption.OnLoad;
  src.EndInit();
  image.Source = src;
  image.Stretch = Stretch.Uniform;
  image.Width = 1024;
  image.Height = 768;
  return image;
}

private void Window_Loaded(object sender, RoutedEventArgs e) {
  var imgA = LoadSource(@"D:\test\1.jpg");
  var imgB = LoadSource(@"D:\test\2.png");
  var bmp = new RenderTargetBitmap(1024, 768, 120, 96, PixelFormats.Pbgra32);
  bmp.Render(imgA);
  bmp.Render(imgB);
  ResultImage.Source = bmp;
}

but it doen't work.

Can anyone point me at the solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幼儿园老大 2024-11-14 09:26:59

我有一个更简单的解决方案:

XAML:

<DockPanel>
    <Button DockPanel.Dock="Bottom" HorizontalAlignment="Right" 
            Margin="12" Click="ButtonSave_OnClick" Content="_Save" />
    <Grid Name="MergedImage" Width="1024" Height="768" 
            HorizontalAlignment="Center" VerticalAlignment="Center" 
            AllowDrop="True" DragOver="MergedImage_DragEnter" 
            Drop="MergedImage_Drop">
        <Image Name="SourcePicture"/>
        <Image Source="Watermark.png"/>
    </Grid>
</DockPanel>

代码隐藏:

void ButtonSave_OnClick(object sender, RoutedEventArgs e)
{
    var dialog = new Microsoft.Win32.SaveFileDialog 
    { 
        DefaultExt = ".jpg", 
        Filter = "Image (.jpg)|*.jpg" 
    };

    if (dialog.ShowDialog() == true)
    {
        // Save my merged image as jpeg.
        // SaveAsJpeg(MergedImage, dialog.FileName);
    }
}

private void MergedImage_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        var fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, true);
        if (fileNames.Length == 1)
        {
            var file = new FileInfo(fileNames[0]);
            if (file.Extension.ToLower() == ".png" ||
                file.Extension.ToLower() == ".bmp" ||
                file.Extension.ToLower() == ".jpg" ||
                file.Extension.ToLower() == ".jpeg" ||
                file.Extension.ToLower() == ".gif")
            {
                e.Effects = DragDropEffects.Copy;
                e.Handled = true;
                return;
            }
        }
    }
    e.Effects = DragDropEffects.None;
    e.Handled = true;
}

private void MergedImage_Drop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        var fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, true);
        SourcePicture.Source = new BitmapImage(new Uri(fileNames[0], 
            UriKind.Absolute));
        Application.Current.MainWindow.Activate();
    }
}

I have an easier solution:

XAML:

<DockPanel>
    <Button DockPanel.Dock="Bottom" HorizontalAlignment="Right" 
            Margin="12" Click="ButtonSave_OnClick" Content="_Save" />
    <Grid Name="MergedImage" Width="1024" Height="768" 
            HorizontalAlignment="Center" VerticalAlignment="Center" 
            AllowDrop="True" DragOver="MergedImage_DragEnter" 
            Drop="MergedImage_Drop">
        <Image Name="SourcePicture"/>
        <Image Source="Watermark.png"/>
    </Grid>
</DockPanel>

Code behind:

void ButtonSave_OnClick(object sender, RoutedEventArgs e)
{
    var dialog = new Microsoft.Win32.SaveFileDialog 
    { 
        DefaultExt = ".jpg", 
        Filter = "Image (.jpg)|*.jpg" 
    };

    if (dialog.ShowDialog() == true)
    {
        // Save my merged image as jpeg.
        // SaveAsJpeg(MergedImage, dialog.FileName);
    }
}

private void MergedImage_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        var fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, true);
        if (fileNames.Length == 1)
        {
            var file = new FileInfo(fileNames[0]);
            if (file.Extension.ToLower() == ".png" ||
                file.Extension.ToLower() == ".bmp" ||
                file.Extension.ToLower() == ".jpg" ||
                file.Extension.ToLower() == ".jpeg" ||
                file.Extension.ToLower() == ".gif")
            {
                e.Effects = DragDropEffects.Copy;
                e.Handled = true;
                return;
            }
        }
    }
    e.Effects = DragDropEffects.None;
    e.Handled = true;
}

private void MergedImage_Drop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        var fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, true);
        SourcePicture.Source = new BitmapImage(new Uri(fileNames[0], 
            UriKind.Absolute));
        Application.Current.MainWindow.Activate();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文