wpfͼƬתobject,wpfͼƬÐýת

http://www.itjxue.com  2023-01-04 19:19  À´Ô´:δ֪  µã»÷´ÎÊý: 

wpfÔõô°ÑÎı¾×ª³ÉͼƬ

RenderTargetBitmap£º Õâ¸öÀà¿ÉÒÔ½«ÈκÎÈÝÆ÷¿Ø¼þäÖȾ³ÉÒ»¸öλͼ

ʾÀý£º ListView livStudent

var?render?=?new?RenderTargetBitmap((int)livStudent.ActualWidth,?(int)livStudent.ActualHeight,?96,?96,?PixelFormats.Pbgra32);

render.Render(livStudent);

BitmapEncoder?encoder?=?new?PngBitmapEncoder();

encoder.Frames.Add(BitmapFrame.Create(render));

using?(var?stream?=?File.Create(@"d:\123.jpg"))

????encoder.Save(stream);

WPFÖРͨ¹ýsocket ½ÓÊÕµ½µÄ JPG ¶þ½øÖÆÁ÷ÈçºÎת³É Image.Source

£¬Ô­À´µÄ³ÌÐòÊÇÓÃwinformдµÄ ͬÑùµÄsocketº¯Êý »ñÈ¡µ½Í¬ÑùµÄ¶þ½øÖÆÁ÷£¬winformÖÐÖ±½ÓʹÓà System.Drawing.Image img = System.Drawing.Image.FromStream(info.MemoryStream);È»ºó½«img¶ÔÏó¸øµ½ pictureBox1.Image = img;ÔÚwpfÖРͼƬ¿Ø¼þÓÐËù¸Ä±ä ʹÓõÄÊÇ Image ¶ÔÏó ¶ÔÓ¦µÄͼƬÊôÐÔÊÇ Source ÀàÐÍÊÇ System.Windows.Media.ImageSource ÕÒÁËһЩת»»·½·¨ ÆäÖÐÒ»¸ö·½·¨ÊÇ£º BitmapImage image = new BitmapImage();

image.BeginInit();

image.StreamSource = info.MemoryStream;

image.EndInit(); ת»»ÎÞЧ Òì³£ÊÇ£ºÎÞ·¨¶Ô´ËͼÏñ½øÐнâÂë¡£¸ÃͼÏñÍ·¿ÉÄÜÒÑË𻵡£ÎÒ·ÖÎöÁËһϠ¹ýÀ´µÄ¶þ½øÖÆÁ÷ʽjpeg¸ñʽµÄ£¬¶ø BitmapImage ×ÖÃæÒâ˼Ӧ¸ÃÊÇλͼͼÏñ£¬ËùÒÔת»»Ê§°Ü¡£¡£ºóÀ´ÔÚ System.Windows.Media.Imaging ÏÂÕÒµ½ÁË Ò»¸ö JpegBitmapDecoder ʹÓ÷½·¨£º BitmapDecoder decoder = JpegBitmapDecoder.Create(info.MemoryStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

image1.Source = decoder.Frames[0]; ͬÑùÎÞЧ¡£¡£Í¼Æ¬»¹Ã»Ã»ÓгöÀ´µ«ÊǶþ½øÖÆÁ÷¿Ï¶¨ÊǶÔÁË ÎÒʹÓÃÁË System.Drawing.Image img = System.Drawing.Image.FromStream(info.MemoryStream);

img.Save("i:\\pic\\" + DateTime.Now.Ticks + "_.jpg");±£´æµ½´ÅÅÌ Í¼Æ¬ÏÔʾÕý³£¡£¸ßÊÖ°ïæ°¡£¡£¡£¡£¡ÏÈл¹ýÁË£¡

c# wpf±³¾°Í¼Æ¬ÔõôÉèÖóÉ×ÊÔ´ÎļþµÄͼƬ

´úÂë¿´ÆðÀ´Ã»ÎÊÌâ°¡£¬ÄãÉèÖÃÒ»ÏÂborderµÄWidthºÍHeightÊôÐÔÊÔÊÔÓÐЧ¹ûÂð¡£

»¹²»ÐеĻ°°ÑÉèÖÃImageSourceµÄ´úÂë¸Ä³ÉÕâÑù¿´¿´¿ÉÒÔÂð

b.ImageSource?=?new?BitmapImage(new?Uri("pack://application:,,,/Resources/px_down.png",?UriKind.RelativeOrAbsolute));

×ÀÃæÓ¦Óà wpf ͼƬÂÖ²¥Éè¼ÆÖÐ Èçͼ£¬ÔõôдÕâ¸öЧ¹û³öÀ´ÄØ£¬ Ï£Íû»áµÄÈ˸øһϿª·¢Ë¼Â· ¿¨×¡ÁË ÐÂÊÖһö

ÕâÖÖÇé¿öÖ»ÄÜ×Ô¼º»æͼ£¬»òÕßʹÓÃͼƬ,ÏÖÔÚ½éÉÜÔõô»æͼ,ÏÈչʾһÏÂЧ¹ûͼ:

µÚÒ»²½:ÏÈ´´½¨Ò»¸ö»æͼÀà,ÒÔÖ§³ÖÍÖÔ²ÐνÇ

?public?class?RoundedCornersPolygon?:?Shape

????{

????????private?readonly?Path?_path;

????????#region?Properties?

????????private?PointCollection?_points;

????????///?summary

????????///?Gets?or?sets?a?collection?that?contains?the?points?of?the?polygon.

????????///?/summary

????????public?PointCollection?Points

????????{

????????????get?{?return?_points;?}

????????????set

????????????{

????????????????_points?=?value;

????????????????RedrawShape();

????????????}

????????}

????????private?bool?_isClosed;

????????///?summary

????????///?Gets?or?sets?a?value?that?specifies?if?the?polygon?will?be?closed?or?not.

????????///?/summary

????????public?bool?IsClosed

????????{

????????????get

????????????{

????????????????return?_isClosed;

????????????}

????????????set

????????????{

????????????????_isClosed?=?value;

????????????????RedrawShape();

????????????}

????????}

????????private?bool?_useRoundnessPercentage;

????????///?summary

????????///?Gets?or?sets?a?value?that?specifies?if?the?ArcRoundness?property?value?will?be?used?as?a?percentage?of?the?connecting?segment?or?not.

????????///?/summary

????????public?bool?UseRoundnessPercentage

????????{

????????????get

????????????{

????????????????return?_useRoundnessPercentage;

????????????}

????????????set

????????????{

????????????????_useRoundnessPercentage?=?value;

????????????????RedrawShape();

????????????}

????????}

????????private?double?_arcRoundness;

????????///?summary

????????///?Gets?or?sets?a?value?that?specifies?the?arc?roundness.

????????///?/summary

????????public?double?ArcRoundness

????????{

????????????get

????????????{

????????????????return?_arcRoundness;

????????????}

????????????set

????????????{

????????????????_arcRoundness?=?value;

????????????????RedrawShape();

????????????}

????????}

????????public?Geometry?Data

????????{

????????????get

????????????{

????????????????return?_path.Data;

????????????}

????????}

????????#endregion

????????public?RoundedCornersPolygon()

????????{

????????????var?geometry?=?new?PathGeometry();

????????????geometry.Figures.Add(new?PathFigure());

????????????_path?=?new?Path?{?Data?=?geometry?};

????????????Points?=?new?PointCollection();

????????????Points.Changed?+=?Points_Changed;

????????}

????????private?void?Points_Changed(object?sender,?EventArgs?e)

????????{

????????????RedrawShape();

????????}

????????#region?Implementation?of?Shape

????????protected?override?Geometry?DefiningGeometry

????????{

????????????get

????????????{

????????????????return?_path.Data;

????????????}

????????}

????????#endregion

????????#region?Private?Methods

????????///?summary

????????///?Redraws?the?entire?shape.

????????///?/summary

????????private?void?RedrawShape()

????????{

????????????var?pathGeometry?=?_path.Data?as?PathGeometry;

????????????if?(pathGeometry?==?null)?return;

????????????var?pathFigure?=?pathGeometry.Figures[0];

????????????pathFigure.Segments.Clear();

????????????for?(int?counter?=?0;?counter??Points.Count;?counter++)

????????????{

????????????????switch?(counter)

????????????????{

????????????????????case?0:

????????????????????????AddPointToPath(Points[counter],?null,?null);

????????????????????????break;

????????????????????case?1:

????????????????????????AddPointToPath(Points[counter],?Points[counter?-?1],?null);

????????????????????????break;

????????????????????default:

????????????????????????AddPointToPath(Points[counter],?Points[counter?-?1],?Points[counter?-?2]);

????????????????????????break;

????????????????}

????????????}

????????????if?(IsClosed)

????????????????CloseFigure(pathFigure);

????????}

????????///?summary

????????///?Adds?a?point?to?the?shape

????????///?/summary

????????///?param?name="currentPoint"The?current?point?added/param

????????///?param?name="prevPoint"Previous?point/param

????????///?param?name="prevPrevPoint"The?point?before?the?previous?point/param

????????private?void?AddPointToPath(Point?currentPoint,?Point??prevPoint,?Point??prevPrevPoint)

????????{

????????????if?(Points.Count?==?0)

????????????????return;

????????????var?pathGeometry?=?_path.Data?as?PathGeometry;

????????????if?(pathGeometry?==?null)?return;

????????????var?pathFigure?=?pathGeometry.Figures[0];

????????????//the?first?point?of?a?polygon

????????????if?(prevPoint?==?null)

????????????{

????????????????pathFigure.StartPoint?=?currentPoint;

????????????}

????????????//second?point?of?the?polygon,?only?a?line?will?be?drawn

????????????else?if?(prevPrevPoint?==?null)

????????????{

????????????????var?lines?=?new?LineSegment?{?Point?=?currentPoint?};

????????????????pathFigure.Segments.Add(lines);

????????????}

????????????//third?point?and?above

????????????else

????????????{

????????????????ConnectLinePoints(pathFigure,?prevPrevPoint.Value,?prevPoint.Value,?currentPoint,?ArcRoundness,?UseRoundnessPercentage);

????????????}

????????}

????????///?summary

????????///?Adds?the?segments?necessary?to?close?the?shape

????????///?/summary

????????///?param?name="pathFigure"/param

????????private?void?CloseFigure(PathFigure?pathFigure)

????????{

????????????//No?need?to?visually?close?the?figure?if?we?don't?have?at?least?3?points.

????????????if?(Points.Count??3)

????????????????return;

????????????Point?backPoint,?nextPoint;

????????????if?(UseRoundnessPercentage)

????????????{

????????????????backPoint?=?GetPointAtDistancePercent(Points[Points.Count?-?1],?Points[0],?ArcRoundness,?false);

????????????????nextPoint?=?GetPointAtDistancePercent(Points[0],?Points[1],?ArcRoundness,?true);

????????????}

????????????else

????????????{

????????????????backPoint?=?GetPointAtDistance(Points[Points.Count?-?1],?Points[0],?ArcRoundness,?false);

????????????????nextPoint?=?GetPointAtDistance(Points[0],?Points[1],?ArcRoundness,?true);

????????????}

????????????ConnectLinePoints(pathFigure,?Points[Points.Count?-?2],?Points[Points.Count?-?1],?backPoint,?ArcRoundness,?UseRoundnessPercentage);

????????????var?line2?=?new?QuadraticBezierSegment?{?Point1?=?Points[0],?Point2?=?nextPoint?};

????????????pathFigure.Segments.Add(line2);

????????????pathFigure.StartPoint?=?nextPoint;

????????}

????????private?static?void?ConnectLinePoints(PathFigure?pathFigure,?Point?p1,?Point?p2,?Point?p3,?double?roundness,?bool?usePercentage)

????????{

????????????//The?point?on?the?first?segment?where?the?curve?will?start.

????????????Point?backPoint;

????????????//The?point?on?the?second?segment?where?the?curve?will?end.

????????????Point?nextPoint;

????????????if?(usePercentage)

????????????{

????????????????backPoint?=?GetPointAtDistancePercent(p1,?p2,?roundness,?false);

????????????????nextPoint?=?GetPointAtDistancePercent(p2,?p3,?roundness,?true);

????????????}

????????????else

????????????{

????????????????backPoint?=?GetPointAtDistance(p1,?p2,?roundness,?false);

????????????????nextPoint?=?GetPointAtDistance(p2,?p3,?roundness,?true);

????????????}

????????????int?lastSegmentIndex?=?pathFigure.Segments.Count?-?1;

????????????//Set?the?ending?point?of?the?first?segment.

????????????((LineSegment)(pathFigure.Segments[lastSegmentIndex])).Point?=?backPoint;

????????????//Create?and?add?the?curve.

????????????var?curve?=?new?QuadraticBezierSegment?{?Point1?=?p2,?Point2?=?nextPoint?};

????????????pathFigure.Segments.Add(curve);

????????????//Create?and?add?the?new?segment.

????????????var?line?=?new?LineSegment?{?Point?=?p3?};

????????????pathFigure.Segments.Add(line);

????????}

????????private?static?Point?GetPointAtDistancePercent(Point?p1,?Point?p2,?double?distancePercent,?bool?firstPoint)

????????{

????????????double?rap?=?firstPoint???distancePercent?/?100?:?(100?-?distancePercent)?/?100;

????????????return?new?Point(p1.X?+?(rap?*?(p2.X?-?p1.X)),?p1.Y?+?(rap?*?(p2.Y?-?p1.Y)));

????????}

????????private?static?Point?GetPointAtDistance(Point?p1,?Point?p2,?double?distance,?bool?firstPoint)

????????{

????????????double?segmentLength?=?Math.Sqrt(Math.Pow((p2.X?-?p1.X),?2)?+?Math.Pow((p2.Y?-?p1.Y),?2));

????????????//The?distance?cannot?be?greater?than?half?of?the?length?of?the?segment

????????????if?(distance??(segmentLength?/?2))

????????????????distance?=?segmentLength?/?2;

????????????double?rap?=?firstPoint???distance?/?segmentLength?:?(segmentLength?-?distance)?/?segmentLength;

????????????return?new?Point(p1.X?+?(rap?*?(p2.X?-?p1.X)),?p1.Y?+?(rap?*?(p2.Y?-?p1.Y)));

????????}

????????#endregion

????}

µÚ¶þ²½:Ç°¶ËXMALÎļþ½øÐÐʹÓÃ,´úÂëÈçÏÂ:

????Grid

????????Grid.RowDefinitions

????????????RowDefinition?Height="20"/RowDefinition

????????????RowDefinition?Height="100"/RowDefinition

????????????RowDefinition?Height="64"/RowDefinition

????????/Grid.RowDefinitions

????????local:RoundedCornersPolygon?Points="7,0?14,8?7,16,0?8"??StrokeThickness="1"?ArcRoundness="1"?UseRoundnessPercentage="False"?Stroke="Black"?IsClosed="True"??HorizontalAlignment="Center"/

????????/local:RoundedCornersPolygon

????????local:RoundedCornersPolygon?Points="14,0?28,16?14,32,0?16"?Grid.Row="1"??StrokeThickness="1"?ArcRoundness="3.5"?UseRoundnessPercentage="False"?Stroke="Black"?IsClosed="True"??HorizontalAlignment="Center"/

????????StackPanel?Grid.Row="2"?Orientation="Horizontal"?HorizontalAlignment="Center"

????????????local:RoundedCornersPolygon?Points="14,0?28,16?14,32,0?16"?Margin="10,10,0,0"?StrokeThickness="1"?ArcRoundness="3.5"?UseRoundnessPercentage="False"?Stroke="Black"?IsClosed="True"/

????????????local:RoundedCornersPolygon?Points="14,0?28,16?14,32,0?16"?Margin="10,10,0,0"??StrokeThickness="1"?ArcRoundness="3.5"?UseRoundnessPercentage="False"?Stroke="Black"?IsClosed="True"?Fill="#ccc"/

????????????local:RoundedCornersPolygon?Points="14,0?28,16?14,32,0?16"?Margin="10,10,0,0"??StrokeThickness="1"?ArcRoundness="3.5"?UseRoundnessPercentage="False"?Stroke="Black"?IsClosed="True"?Fill="#ccc"/

????????/StackPanel

????/Grid

WPF ½«Í¼Æ¬±£´æΪXMLÎļþ

Õâ¸ö£¬ÆäʵºÜ¼òµ¥µÄ¡£¾ÍÊÇÒ»¸öbase64µÄ±àÂëºÍ½âÂë¹ý³Ì¡£

ÎÒÌØÒâΪÄãдÁËʵÀý£¬ÔÚÎҵIJ©¿ÍÀµØÖ·ÈçÏ£º

WPFÖÐBitmapImage¶ÔÏóÇиî

ÔÚWPFÖпÉÒÔʹÓÃInt32RectÀ´ÇиîͼƬ£¬µ±È»ÄãҪת»»Ò²¿ÉÒÔ£¬¸Õ¸Õ»Ø¸´ÁËÄãµÄÆÀÂÛ£¬Ð´µÃºÜ¼òÂÔ£¬ÕâÀï¸øÄãÒ»¸öת»»µÄ·½·¨°É£º

Bitmap BitmapSourceToBitmap(BitmapSource source)

{

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(source.PixelWidth, source.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

source.CopyPixels(Int32Rect.Empty, bitmapData.Scan0, bitmapData.Height * bitmapData.Stride, bitmapData.Stride);

bitmap.UnlockBits(bitmapData);

return bitmap;

}

Ï£Íû¶ÔÄãÓаïÖú£¬ÓÐÒÉÎÊÇë×·ÎÊ»òÊÇHi

(ÔðÈα༭£ºIT½ÌѧÍø)

¸ü¶à