什么是VB.NET的等效语法和QUOT;产量与收益;?QUOT [英] What is the equivalent syntax in VB.NET for "yield return"?
本文介绍了什么是VB.NET的等效语法和QUOT;产量与收益;?QUOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用下面的 C#代码,你将如何把它写在Visual Basic中?这是什么?想说
使用系统;
使用System.Collections.Generic;
:使用System.IO;使用System.Windows
;使用System.Windows.Media
;
使用System.Windows.Media.Imaging;
命名空间Microsoft.LiveLabs.Pivot
{
///<总结>
///瓷砖生成器类
///< /总结>
公共静态类TileBuilder
{
///<总结>
///指定所需要的CreateTile
所使用的图像阵列中的图像///根据由海龙使用的莫顿分形图案。
///< /总结>
///<&言论GT;这种
///用法CreateTile是棘手的一种。这里有一个例子:
///假设你有一个结果集是像这样的项目的集合:{项目1,项目2,...,item100}
///由于海龙预计瓷砖在莫顿的模式进行布局,
/// 6级将类似于以下内容:
///
/// ------------- -------------
/// | 0 1 4 5 | 16 17 ...
/// | 2 3 6 7 | 18 19
/// | 8 9 12 13 | 24 25
/// | 10 11 14 15 | 26 27
/// | -----------------------
/// | 32 33 36 37 | 48 49
/// | 34 35 38 39 | 50 51 ...
/// |。 。
/// |。 。
///。 。
///
///处第6级每瓦为4×4,所以破折号代表瓷砖的边界。现在,假设///要构建0,0
。你需要的瓷砖图像。 IDS的0,1,4,5 ...
///表示在结果集中的ID,{项目1,项目2,...,item100}。调用
///此方法告诉你的ID,以用于给定的瓷砖。然后,您必须检索
///出来的结果集所必需的映像,并在此
///方法给你CreateTile的顺序提供它们。这将导致一个正确构建瓦片
///该海龙可以使用。
///< /言论>
///< PARAM NAME =imageCount>图片中的全套号码< /参数>
///< PARAM NAME =级别>至每个图像将被下采样水平< /参数>
///< PARAM NAME =行>它指定了图像渲染的行号< /参数>
///< PARAM NAME =列>它指定了图像渲染的行号< /参数>
///< PARAM NAME =tileSize>在瓷砖返回的大小< /参数>
公共静态的IEnumerable< INT> GetTileIds(
INT imageCount,
INT水平,
INT行,
柱INT,
INT tileSize)
{
//计算上在图像空间瓷砖-left角落(1单位= 1图像)
INT levelSize =(int)的Math.Pow(2级);
INT imagePerSide = tileSize / levelSize;
INT xOffset =行* imagePerSide;
INT yOffset =列* imagePerSide;
如果(imagePerSide&下; = 0)
{
抛出新ArgumentOutOfRangeException(等级高于由瓦片大小所允许的最大深度大于或瓦片大小为0。 );
}
//循环通过x和y中图像的空间,开始区块的左上角
//角落。发现在给定的瓷砖所有ID。
的for(int x = 0; X< imagePerSide; X ++)
{
为(INT Y = 0; Y< imagePerSide; Y ++)
{
INT N = XYToMorton(X + xOffset,Y + yOffset);
如果(N< imageCount)
{
收益率的回报N;
}
}
}
}
///<总结>
///根据海龙使用的莫顿分
///模式的集合创建一个瓦。
///< /总结>
///<&言论GT;
///见GetTileIds获取更多信息。
///< /言论>
///&下; PARAM NAME =imageCount>图像集合中的总数与所述; /参数>
///< PARAM NAME =图片> JPEG图像上呈现这种瓷砖。
///如果这是空,一个空块将被退回。
///见GetTileIds言论更多信息,与LT; /参数>
///< PARAM NAME =级别>至每个图像将被下采样水平< /参数>
///< PARAM NAME =行>它指定了图像渲染的行号< /参数>
///< PARAM NAME =列>它指定了图像渲染的行号< /参数>
///< PARAM NAME =tileSize>在瓷砖返回的大小< /参数>
///&下; PARAM NAME =输出>将流用,以输出结果&下; /参数>
公共静态无效CreateTile(
INT imageCount,
的IEnumerable< ImageBag>的图像,
INT水平,
INT行,
INT列,
INT tileSize,
串的fileType,
流输出)
{
//计算图像空间瓷砖的上层左下角(1单位= 1的图像)。
INT levelSize =(int)的Math.Pow(2级);
INT imagePerSide = tileSize / levelSize;
INT xOffset =行* imagePerSide;
INT yOffset =列* imagePerSide;
如果(imagePerSide&下; = 0)
{
抛出新ArgumentOutOfRangeException(等级高于由瓦片大小所允许的最大深度大于或瓦片大小为0。 );
}
如果(输出== NULL)
{
抛出新的ArgumentNullException(给定输出流为空);
}
//创建瓷砖。
WriteableBitmap的outputBitmap =新的WriteableBitmap的(
tileSize,
tileSize,
96,
96,
PixelFormats.Bgr24,
NULL);
//如果图像为null,则返回一个空块。
如果(照片!= NULL)
{
//循环相对x和y的图像空间的瓷砖。
&的IEnumerator LT; ImageBag> imageEnumerator = images.GetEnumerator();
的for(int x = 0; X< imagePerSide; X ++)
{
为(INT Y = 0; Y< imagePerSide; Y ++)
{
//转换为从绝对图像空间莫顿ID空间(以获得绝对的,加偏移)。
INT N = XYToMorton(X + xOffset,Y + yOffset);
如果(N< imageCount)
{
如果(imageEnumerator.MoveNext())
{
如果(imageEnumerator.Current == NULL)
{
继续;
}
//计算像素位置
INT locX = levelSize * X;
INT locY = levelSize * Y;
INT宽度= 0;
INT高度= 0;
imageEnumerator.Current.ImageSize(出宽度,高度进行);
的MemoryStream的ImageStream =新的MemoryStream(imageEnumerator.Current.ImageData);
//确定最大的瓷砖尺寸的二为
最近的电源//这个图片:2 ^ CEIL(LG MAX(宽,高))。
双maxTileSize = Math.Pow(2,Math.Ceiling(将Math.log(Math.Max(宽度,高度),2)));
//下采样到正确的大小并解压缩图像。正确的尺寸
//是
//总宽度所需的图像*水平尺寸/最大尺寸的瓷砖总维度。把这看作由两个的foreach
//水平分割尺寸,在最大尺寸的瓷砖开始,并持续上涨到目前的
//平铺尺寸
TransformedBitmap downsampledImage = JpegDecoder.DownsampleJpeg(
的ImageStream,
Math.Ceiling(宽* levelSize / maxTileSize),
Math.Ceiling(高* levelSize / maxTileSize));
//像素复制到缓冲区中,然后将它们写入到
//适当的区域输出的图像上。
INT跨距=(downsampledImage.PixelWidth * downsampledImage.Format.BitsPerPixel + 7)/ 8;
字节[]缓冲区=新的字节[步幅* downsampledImage.PixelHeight]。
downsampledImage.CopyPixels(缓冲器,步幅,0);
Int32Rect outputRect =新Int32Rect(locX,locY,downsampledImage.PixelWidth,downsampledImage.PixelHeight);
outputBitmap.WritePixels(outputRect,缓冲器,步幅,0);
}
,否则
{
//我们应该呈现的形象,但我们有我们的名单进行。
//所以,退出这两个循环。
X = imagePerSide;
Y = imagePerSide;
}
}
,否则
{
//由于n单调WRT Y,我们知道Y已走得太远了,所以
//我们可以重置。
Y = imagePerSide;
}
}
}
}
//写输出
BitmapFrame outputFrame = BitmapFrame.Create(outputBitmap);
的BitmapEncoder编码器=新JpegBitmapEncoder();
encoder.Frames.Add(outputFrame);
encoder.Save(输出);
}
///<总结>
///一个x和y到莫顿数
///<转换; /总结>
///&下; PARAM NAME =×>到转换x位置及下; /参数>
///< PARAM NAME =Y> y位置转换< /参数>
///&下;返回>返回其对应于
中的莫顿数///给定的x和y坐标与所述; /回报>
私有静态诠释XYToMorton(INT X,int y)对
{
常量UINT BITS_PER_BYTE = 8;
常量UINT BIT_PAIRS = sizeof的(INT)* BITS_PER_BYTE / 2;
INT莫顿= 0;
的for(int i = 0; I< BIT_PAIRS;我++)
{
莫顿| =(X安培; 1) - =;< (我* 2);
莫顿| =(Y&放大器; 1) - ;&下; ((ⅰ* 2)+ 1);
X - GT;> = 1;
Y形>> = 1;
}
返回莫顿;
}
}
}
解决方案
C#:
收益回报scriptRef
VB.NET:
返回新的列表(中的ScriptReference)从{scriptRef }
Using the C# code below, how would you write it in Visual Basic? What is it trying to say?
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Microsoft.LiveLabs.Pivot
{
/// <summary>
/// Tile Builder class
/// </summary>
public static class TileBuilder
{
/// <summary>
/// Specifies which images are required in the images array used in CreateTile
/// according to the Morton fractal pattern used by Seadragon.
/// </summary>
/// <remarks>
/// Usage of this and CreateTile are kind of tricky. Here's an example:
/// Say you have a results set that is a collection of items like so: { item1, item2, ..., item100 }
/// Since Seadragon expects the tiles to be laid out in a Morton pattern,
/// level 6 will look like the following:
///
/// --------------------------
/// |0 1 4 5 | 16 17...
/// |2 3 6 7 | 18 19
/// |8 9 12 13 | 24 25
/// |10 11 14 15 | 26 27
/// |-----------------------
/// |32 33 36 37 | 48 49
/// |34 35 38 39 | 50 51...
/// |. .
/// |. .
/// . .
///
/// Each tile at level 6 is 4x4, so the dashes represent tile boundaries. Now, say
/// you want to build 0,0. You need the images on that tile. The ids 0, 1, 4, 5...
/// represent the ids in your result set, { item1, item2, ..., item100 }. Calling
/// this method tells you the ids to use for a given tile. You then must retrieve
/// the necessary images out the result set, and supply them in the order this
/// method gave you to CreateTile. This will result in a correctly built tile
/// that Seadragon can use.
/// </remarks>
/// <param name="imageCount">Number of images in the full set.</param>
/// <param name="level">The level to which each image will be downsampled.</param>
/// <param name="row">The row number which specifies what images to render.</param>
/// <param name="column">The row number which specifies what images to render.</param>
/// <param name="tileSize">The size of the tile to return.</param>
public static IEnumerable<int> GetTileIds(
int imageCount,
int level,
int row,
int column,
int tileSize)
{
// Calculate upper-left hand corner of tile in image space (1 unit = 1 image)
int levelSize = (int)Math.Pow(2, level);
int imagePerSide = tileSize / levelSize;
int xOffset = row * imagePerSide;
int yOffset = column * imagePerSide;
if (imagePerSide <= 0)
{
throw new ArgumentOutOfRangeException("Level is greater than the maximum depth allowed by the tile size, or the tile size is 0.");
}
// Loop through x and y in image space, starting at the upper-left
// hand corner of the tile. Find all ids on the given tile.
for (int x = 0; x < imagePerSide; x++)
{
for (int y = 0; y < imagePerSide; y++)
{
int n = XYToMorton(x + xOffset, y + yOffset);
if (n < imageCount)
{
yield return n;
}
}
}
}
/// <summary>
/// Create a tile for a collection according to the Morton fractal
/// pattern used by Seadragon.
/// </summary>
/// <remarks>
/// See GetTileIds for more information.
/// </remarks>
/// <param name="imageCount">The total number of images in the collection.</param>
/// <param name="images">Jpeg images to render on this tile.
/// If this is null, a blank tile will be returned.
/// See GetTileIds remarks for more information.</param>
/// <param name="level">The level to which each image will be downsampled.</param>
/// <param name="row">The row number which specifies what images to render.</param>
/// <param name="column">The row number which specifies what images to render.</param>
/// <param name="tileSize">The size of the tile to return.</param>
/// <param name="output">The stream to use to output the result.</param>
public static void CreateTile(
int imageCount,
IEnumerable<ImageBag> images,
int level,
int row,
int column,
int tileSize,
string fileType,
Stream output)
{
// Calculate upper-left hand corner of tile in image space (1 unit = 1 image).
int levelSize = (int)Math.Pow(2, level);
int imagePerSide = tileSize / levelSize;
int xOffset = row * imagePerSide;
int yOffset = column * imagePerSide;
if (imagePerSide <= 0)
{
throw new ArgumentOutOfRangeException("Level is greater than the maximum depth allowed by the tile size, or the tile size is 0.");
}
if (output == null)
{
throw new ArgumentNullException("The given output stream is null.");
}
// Create the tile.
WriteableBitmap outputBitmap = new WriteableBitmap(
tileSize,
tileSize,
96,
96,
PixelFormats.Bgr24,
null);
// If images is null, return a blank tile.
if (images != null)
{
// Loop through the tile in relative x and y image-space.
IEnumerator<ImageBag> imageEnumerator = images.GetEnumerator();
for (int x = 0; x < imagePerSide; x++)
{
for (int y = 0; y < imagePerSide; y++)
{
// Convert to Morton id-space from the absolute image-space (to get absolute, add offsets).
int n = XYToMorton(x + xOffset, y + yOffset);
if (n < imageCount)
{
if (imageEnumerator.MoveNext())
{
if (imageEnumerator.Current == null)
{
continue;
}
// Compute the pixel location
int locX = levelSize * x;
int locY = levelSize * y;
int width = 0;
int height = 0;
imageEnumerator.Current.ImageSize(out width, out height);
MemoryStream imageStream = new MemoryStream(imageEnumerator.Current.ImageData);
// Determine the largest tile size to the nearest power of two for
// this image: 2^ceil(lg max(width, height)).
double maxTileSize = Math.Pow(2, Math.Ceiling(Math.Log(Math.Max(width, height), 2)));
// Downsample to the correct size and decompress the image. The correct size
// is total dimenion of the image * level size / max tile size required for
// total width. Think of this as dividing the dimensions by two foreach
// levels, starting at the max tile size, and going up to the current
// tile size
TransformedBitmap downsampledImage = JpegDecoder.DownsampleJpeg(
imageStream,
Math.Ceiling(width * levelSize / maxTileSize),
Math.Ceiling(height * levelSize / maxTileSize));
// Copy the pixels to a buffer and then write them to the
// appropriate region on the output image.
int stride = (downsampledImage.PixelWidth * downsampledImage.Format.BitsPerPixel + 7) / 8;
byte[] buffer = new byte[stride * downsampledImage.PixelHeight];
downsampledImage.CopyPixels(buffer, stride, 0);
Int32Rect outputRect = new Int32Rect(locX, locY, downsampledImage.PixelWidth, downsampledImage.PixelHeight);
outputBitmap.WritePixels(outputRect, buffer, stride, 0);
}
else
{
// We should render the image, but we're done with our list.
// So, exit both loops.
x = imagePerSide;
y = imagePerSide;
}
}
else
{
// Since n is monotonic wrt y, we know y has gone too far down, so
// we can reset it.
y = imagePerSide;
}
}
}
}
// Write the output
BitmapFrame outputFrame = BitmapFrame.Create(outputBitmap);
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(outputFrame);
encoder.Save(output);
}
/// <summary>
/// Converts an x and y to a Morton number
/// </summary>
/// <param name="x">x location to convert.</param>
/// <param name="y">y location to convert.</param>
/// <returns>Returns the morton number which corresponds to the
/// given x and y coordinates.</returns>
private static int XYToMorton(int x, int y)
{
const uint BITS_PER_BYTE = 8;
const uint BIT_PAIRS = sizeof(int) * BITS_PER_BYTE / 2;
int morton = 0;
for (int i = 0; i < BIT_PAIRS; i++)
{
morton |= (x & 1) << (i * 2);
morton |= (y & 1) << ((i * 2) + 1);
x >>= 1;
y >>= 1;
}
return morton;
}
}
}
解决方案
C#:
yield return scriptRef
VB.NET:
Return New List(Of ScriptReference) From {scriptRef}
这篇关于什么是VB.NET的等效语法和QUOT;产量与收益;?QUOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文