本文整理汇总了C#中System.Drawing.Bitmap.ToBase64方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.ToBase64方法的具体用法?C# Bitmap.ToBase64怎么用?C# Bitmap.ToBase64使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Bitmap
的用法示例。
在下文中一共展示了Bitmap.ToBase64方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Index
public ActionResult Index(string Data, int Smallest = 3, int Largest = 6, HttpPostedFileBase file = null)
{
string base64Image = null;
if (file != null)
{
//try to determine data from posted file
var bitmap = new Bitmap(file.InputStream);
// lock image
BitmapData bitmapData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// step 1 - turn background to black
var filter = new Invert();
filter.ApplyInPlace(bitmapData);
ColorFiltering colorFilter = new ColorFiltering();
colorFilter.Red = new IntRange(0, 64);
colorFilter.Green = new IntRange(0, 64);
colorFilter.Blue = new IntRange(0, 64);
colorFilter.FillOutsideRange = false;
colorFilter.ApplyInPlace(bitmapData);
// step 2 - locating objects
BlobCounter blobCounter = new BlobCounter();
blobCounter.FilterBlobs = true;
blobCounter.MinHeight = 5;
blobCounter.MinWidth = 5;
blobCounter.ProcessImage(bitmapData);
var blobs = blobCounter.GetObjectsInformation();
bitmap.UnlockBits(bitmapData);
base64Image = bitmap.ToBase64();
// get information about detected objects
var shapeChecker = new SimpleShapeChecker();
var letters = new List<Letter>();
int circleCount = 0;
foreach (
var blob in
blobs.ToArray()
.OrderBy(b => b.Rectangle.Top)
.ThenBy(b => b.Rectangle.Left)
.ThenByDescending(b => b.Area))
{
List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
AForge.Point center;
float radius;
if (shapeChecker.IsCircle(edgePoints, out center, out radius))
{
//Todo: filter on the largest radius * 90% to deal with resolutions
if (radius < 40)
continue;
blobCounter.ExtractBlobsImage(bitmap, blob, false);
var letter = blob.Image.ToManagedImage(true);
var resizeFilter = new ResizeBilinear(150, 150);
letter = resizeFilter.Apply(letter);
var bwBitmap = new Bitmap(75, 75, PixelFormat.Format32bppArgb);
for (int y = 40; y < 115; y++)
{
for (int x = 40; x < 115; x++)
{
var color = letter.GetPixel(x, y);
if (color.Name == "ff000000")
{
bwBitmap.SetPixel(x - 40, y - 40, Color.Black);
}
else
{
bwBitmap.SetPixel(x - 40, y - 40, Color.White);
}
}
}
letters.Add(new Letter
{
L = TrainingData.GetBestGuess(bwBitmap),
X = center.X,
Y = center.Y,
Radius = radius
});
}
}
var minX = letters.Min(c => c.X);
var maxX = letters.Max(c => c.X);
var minY = letters.Min(c => c.Y);
//.........这里部分代码省略.........
示例2: Filter
/// <summary>
/// Filters the assets
/// </summary>
/// <param name="Assets">Assets to filter</param>
/// <returns>The filtered assets</returns>
public IList<IAsset> Filter(IList<IAsset> Assets)
{
if (Assets == null || Assets.Count == 0)
return new List<IAsset>();
if (Assets.FirstOrDefault().Type != AssetType.CSS)
return Assets;
FileManager FileSystem = BatComputer.Bootstrapper.Resolve<FileManager>();
foreach (IAsset Asset in Assets)
{
foreach (Match ImageMatch in ImageRegex.Matches(Asset.Content))
{
string TempFile = ImageMatch.Groups["File"].Value;
string MatchString = ImageMatch.Value;
IFile File = FileSystem.File(TempFile);
File = DetermineFile(File, FileSystem, Asset, TempFile);
if (File == null || !File.Exists)
{
IFile AssetFile = FileSystem.File(Asset.Path);
File = FileSystem.File(AssetFile.Directory.FullName + "\\" + TempFile);
}
if (File.Exists)
{
if (File.Extension.ToUpperInvariant() == ".TTF"
|| File.Extension.ToUpperInvariant() == ".OTF"
|| File.Extension.ToUpperInvariant() == ".WOFF"
|| File.Extension.ToUpperInvariant() == ".SVG"
|| File.Extension.ToUpperInvariant() == ".EOT")
{
string MIME = "";
if (File.Extension.ToUpperInvariant() == ".WOFF")
MIME = "application/x-font-woff";
else if (File.Extension.ToUpperInvariant() == ".OTF")
MIME = "application/x-font-opentype";
else if (File.Extension.ToUpperInvariant() == ".TTF")
MIME = "application/x-font-ttf";
else if (File.Extension.ToUpperInvariant() == ".SVG")
MIME = "image/svg+xml";
else if (File.Extension.ToUpperInvariant() == ".EOT")
MIME = "application/vnd.ms-fontobject";
Asset.Content = Asset.Content.Replace(MatchString, "url(data:" + MIME + ";base64," + File.ReadBinary().ToString(Base64FormattingOptions.None) + ")");
}
else
{
try
{
using (Bitmap TempImage = new Bitmap(File.FullName))
{
string MIMEType = "image/jpeg";
string Content = "";
if (File.FullName.ToUpperInvariant().EndsWith(".PNG"))
{
MIMEType = "image/png";
Content = TempImage.ToBase64(ImageFormat.Png);
}
else if (File.FullName.ToUpperInvariant().EndsWith(".JPG") || File.FullName.ToUpperInvariant().EndsWith(".JPEG"))
{
MIMEType = "image/jpeg";
Content = TempImage.ToBase64(ImageFormat.Jpeg);
}
else if (File.FullName.ToUpperInvariant().EndsWith(".GIF"))
{
MIMEType = "image/gif";
Content = TempImage.ToBase64(ImageFormat.Gif);
}
else if (File.FullName.ToUpperInvariant().EndsWith(".TIFF"))
{
MIMEType = "image/tiff";
Content = TempImage.ToBase64(ImageFormat.Tiff);
}
else if (File.FullName.ToUpperInvariant().EndsWith(".BMP"))
{
MIMEType = "image/bmp";
Content = TempImage.ToBase64(ImageFormat.Bmp);
}
Asset.Content = Asset.Content.Replace(MatchString, "url(data:" + MIMEType + ";base64," + Content + ")");
}
}
catch { }
}
}
}
}
return Assets;
}
示例3: Process
public ActionResult Process(HttpPostedFileBase file)
{
var model = new List<TrainingDataItem>();
var bitmap = new Bitmap(file.InputStream);
// lock image
BitmapData bitmapData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// step 1 - turn background to black
var filter = new Invert();
filter.ApplyInPlace(bitmapData);
ColorFiltering colorFilter = new ColorFiltering();
colorFilter.Red = new IntRange(0, 64);
colorFilter.Green = new IntRange(0, 64);
colorFilter.Blue = new IntRange(0, 64);
colorFilter.FillOutsideRange = false;
colorFilter.ApplyInPlace(bitmapData);
// step 2 - locating objects
BlobCounter blobCounter = new BlobCounter();
blobCounter.FilterBlobs = true;
blobCounter.MinHeight = 5;
blobCounter.MinWidth = 5;
blobCounter.ProcessImage(bitmapData);
var blobs = blobCounter.GetObjectsInformation();
bitmap.UnlockBits(bitmapData);
// get information about detected objects
var shapeChecker = new SimpleShapeChecker();
int circleCount = 0;
foreach (
var blob in
blobs.ToArray()
.OrderBy(b => b.Rectangle.Top)
.ThenBy(b => b.Rectangle.Left)
.ThenByDescending(b => b.Area))
{
List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
AForge.Point center;
float radius;
if (shapeChecker.IsCircle(edgePoints, out center, out radius))
{
//Todo: filter on the largest radius * 90% to deal with resolutions
if (radius < 40)
continue;
blobCounter.ExtractBlobsImage(bitmap, blob, false);
var letter = blob.Image.ToManagedImage(true);
var resizeFilter = new ResizeBilinear(150, 150);
letter = resizeFilter.Apply(letter);
var base64 = (letter.ToBase64());
var sb = new StringBuilder();
var bwBitmap = new Bitmap(75, 75, PixelFormat.Format32bppArgb);
for (int y = 40; y < 115; y++)
{
for (int x = 40; x < 115; x++)
{
var color = letter.GetPixel(x, y);
if (color.Name == "ff000000")
{
sb.Append("0");
bwBitmap.SetPixel(x - 40, y - 40, Color.Black);
}
else
{
sb.Append("1");
bwBitmap.SetPixel(x - 40, y - 40, Color.White);
}
}
sb.AppendLine();
}
model.Add(new TrainingDataItem
{
Base64Image = (bwBitmap.ToBase64()),
Letter = TrainingData.GetBestGuess(bwBitmap)
});
}
}
return View(model.ToArray());
}