writeablebitmap 分割图片

sungoco2 2009-11-09 01:52:50
我使用writeablebitmap将所要截取的图片像素一个一个存到byte[]里,然后使用png编码将其转成stream,成功的完成了截取,由于我在每一侦中都需要截取。因此我的cpu直线上升到11%,由于sl刚入门,想知道 有没有什么其他更简单的方法。
...全文
429 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kntao 2010-06-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jv9 的回复:]
private void Screenshot_Click(object sender, System.Windows.RoutedEventArgs e)
{
// 创建一个WriteableBitmap并且把需要呈现位图的元素赋值给WriteableBitmap
WriteableBitmap wb = new WriteableBitmap(myMediaElement, ……
[/Quote]
载入到Image后,怎样再能打印出来呢?
jv9 2009-11-11
  • 打赏
  • 举报
回复
private void Screenshot_Click(object sender, System.Windows.RoutedEventArgs e)
{
// 创建一个WriteableBitmap并且把需要呈现位图的元素赋值给WriteableBitmap
WriteableBitmap wb = new WriteableBitmap(myMediaElement, null);

// 创建一个Image元素来承载位图
Image image = new Image();
image.Height = 64;
image.Margin = new Thickness(5);
image.Source = wb;

// 将Image元素放入容器控件中
thumbs.Children.Add(image);

ScreenshotViewer.ScrollToVerticalOffset(ScreenshotViewer.ExtentHeight);

}



获取后,直接载入容器了。没有进行数组存取。不过,Silverlight大量的图形处理是很消耗内存和CPU,这个问题有人在Silverlight 4 wish list提出过了,希望以后能有改进。
sungoco2 2009-11-10
  • 打赏
  • 举报
回复
用这个貌似可以解决:
private BitmapSource cutImage(int x, int y, int width, int height)
{
WriteableBitmap bit = new WriteableBitmap(width, height);
TranslateTransform tran = new TranslateTransform();
tran.X = -x;
tran.Y = -y;
bit.Render(image, tran);
bit.Invalidate();

return bit;
}

image是一个Image
这么搞,cpu 0,mem 涨到一定的程度就下来了。这个方法唯一的坏处就是内存会一直涨,涨到一个未知点才会下来。(c#的内存管理啊~~~不适应)完全不可控,这对于习惯了native code的我很不适应。
期待更多好的方法。能够控制cpu和mem

sungoco2 2009-11-10
  • 打赏
  • 举报
回复
你这段代码哪里能获取???
我现在要(150, 300)这个像素点开始的150*150大小的图片,你这个不能实现阿
jv9 2009-11-10
  • 打赏
  • 举报
回复
http://silverlightchina.net/html/tips/2009/1019/211.html

http://silverlightchina.net/plus/view.php?aid=183
jv9 2009-11-10
  • 打赏
  • 举报
回复

<Grid x:Name="LayoutRoot">
<Image x:Name="optImage" Margin="271,8,0,0" VerticalAlignment="Top" Width="240" Stretch="None" Height="143" HorizontalAlignment="Left"/>
<StackPanel HorizontalAlignment="Left" Width="267" Orientation="Vertical" d:LayoutOverrides="Height">
<MediaElement x:Name="myMediaElement" Source="ice.mp4" Stretch="None" Height="132" Margin="0" CurrentStateChanged="myMediaElement_CurrentStateChanged"/>
<StackPanel Margin="0" Orientation="Vertical" d:LayoutOverrides="Width">
<Button x:Name="Screenshot" Content="Screenshot" Click="Screenshot_Click"/>
<ScrollViewer x:Name="ScreenshotViewer" Height="325" Margin="0">
<StackPanel x:Name="thumbs"/>
</ScrollViewer>
</StackPanel>
</StackPanel>
</Grid>



private void Screenshot_Click(object sender, System.Windows.RoutedEventArgs e)
{
// 创建一个WriteableBitmap并且把需要呈现位图的元素赋值给WriteableBitmap
WriteableBitmap wb = new WriteableBitmap(myMediaElement, null);

// 创建一个Image元素来承载位图
Image image = new Image();
image.Height = 64;
image.Margin = new Thickness(5);
image.Source = wb;

// 将Image元素放入容器控件中
thumbs.Children.Add(image);

ScreenshotViewer.ScrollToVerticalOffset(ScreenshotViewer.ExtentHeight);

}



多次呈现
这里使用一个Timer来多次呈现位图(要使用Render方法需要调用Invalidate来呈现位图)



DispatcherTimer timer;
WriteableBitmap wbopt;
public MainPage()
{
InitializeComponent();

timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(10);
timer.Tick += new EventHandler(Render_Video);
wbopt = new WriteableBitmap(240,143);
optImage.Source = wbopt;

timer.Start();
}

void Render_Video(object sender, EventArgs e)
{
wbopt.Render(myMediaElement, null);
wbopt.Invalidate();
}
diandian82 2009-11-09
  • 打赏
  • 举报
回复
mark

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧