111,094
社区成员




public MainWindow()
{
InitializeComponent();
string input_filepath=@"C:\Users\hongxu.lin\Desktop\user_blue.png";
string output_filepath=@"C:\Users\hongxu.lin\Desktop\user_blue1.png";
Process(input_filepath, output_filepath);
}
bool Process(string input_filepath, string output_filepath)
{
// load source image
// BitmapSource bitmap = new BitmapImage(new Uri(input_filepath));
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(input_filepath);
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
// create render bitmap
var rtbitmap = new RenderTargetBitmap(bitmap.PixelWidth,
bitmap.PixelHeight,
bitmap.DpiX,
bitmap.DpiY,
PixelFormats.Default);
// draw watermark
var drawingVisual = new DrawingVisual();
using (var dc = drawingVisual.RenderOpen())
{
// draw source image
dc.DrawImage(bitmap, new Rect(0, 0, bitmap.Width, bitmap.Height));
//draw path
PathGeometry triangle = new PathGeometry();
PathFigure pf = new PathFigure();
pf.StartPoint = new Point(-bitmap.Width / 2, bitmap.Width/2);
pf.Segments.Add(new LineSegment(new Point(bitmap.Width, bitmap.Width), true));
pf.Segments.Add(new LineSegment(new Point(bitmap.Width, -bitmap.Width), true));
triangle.Figures.Add(pf);
dc.DrawGeometry(
Brushes.Red,
null,
triangle
);
}
rtbitmap.Render(drawingVisual);
var bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(rtbitmap));
// save image
if (File.Exists(output_filepath))
File.Delete(output_filepath);
using (FileStream file = new FileStream(output_filepath, FileMode.Create))
bitmapEncoder.Save(file);
// return
return true;
}
bool Process(string input_filepath, string output_filepath) // <-- let upper logic determine the output filename, these will provide flexibility
{
// load source image
// BitmapSource bitmap = new BitmapImage(new Uri(input_filepath));
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(input_filepath);
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
// create render bitmap
var rtbitmap = new RenderTargetBitmap(bitmap.PixelWidth,
bitmap.PixelHeight,
bitmap.DpiX,
bitmap.DpiY,
PixelFormats.Default);
// draw watermark
var drawingVisual = new DrawingVisual();
using (var dc = drawingVisual.RenderOpen())
{
// draw source image
dc.DrawImage(bitmap, new Rect(0, 0, bitmap.Width, bitmap.Height));
// determine the render size of the watermark
double hs = bitmap.Height * 0.3 / this._waterPic.Height;
double ws = bitmap.Width * 0.3 / this._waterPic.Width;
double scale = hs > ws ? ws : hs;
if (bitmap.Height / bitmap.Width > 5)
{
scale = bitmap.Width * 0.8 / this._waterPic.Width;
}
else if (bitmap.Width / bitmap.Height > 5)
{
scale = bitmap.Height * 0.8 / this._waterPic.Height;
}
double Wstart = bitmap.Width - this._waterPic.Width * scale - 20 < 0 ? 0 : bitmap.Width - this._waterPic.Width * scale - 20;
double Hstart = bitmap.Height - this._waterPic.Height * scale;
// draw water make
dc.DrawImage(this._waterPic, new Rect(Wstart, Hstart, this._waterPic.Width * scale, this._waterPic.Height * scale));
}
rtbitmap.Render(drawingVisual);
var bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(rtbitmap));
// save image
if (File.Exists(output_filepath))
File.Delete(output_filepath);
using (FileStream file = new FileStream(output_filepath, FileMode.Create))
bitmapEncoder.Save(file);
// return
return true;
}