我有以下代码,它接受一个字符串并将其添加到内存中的Bitmap,而Bitmap又保存为BMP文件.我现在的代码如下:
string sFileData = "Hello World";
string sFileName = "Bitmap.bmp";
Bitmap oBitmap = new Bitmap(1,1);
Font oFont = new Font("Arial", 11, FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
int iWidth = 0;
int iHeight = 0;
using (Graphics oGraphics = Graphics.FromImage(oBitmap))
{
oGraphics.Clear(Color.White);
iWidth = (int)oGraphics.MeasureString(sFileData, oFont).Width;
iHeight = (int)oGraphics.MeasureString(sFileData, oFont).Height;
oBitmap = new Bitmap(oBitmap, new Size(iWidth, iHeight));
oGraphics.DrawString(sFileData, oFont, new SolidBrush(System.Drawing.Color.Black), 0, 0);
oGraphics.Flush();
}
oBitmap.Save(sFileName, System.Drawing.Imaging.ImageFormat.Bmp);
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是当我在Paint中查看BMP文件时,位图的大小是正确定义的,背景是白色的,但是它们不是文本?
我究竟做错了什么 ?
string pathFile = @"C:\Users\user\Downloads\CaptchaCollection\Small\Sorted\";
var files = Directory.GetFiles(pathFile).Select(nameWithExtension => Path.GetFileNameWithoutExtension(nameWithExtension)).Where(name => { int number; return int.TryParse(name, out number); }).Select(name => int.Parse(name)).OrderBy(number => number).ToArray();
List<int> fileList = files.ToList();
image1 = new Bitmap(pathFile + fileList[0].ToString() + ".png");
image2 = new Bitmap(pathFile + fileList[1].ToString() + ".png");
if (compare(image2, image2))
{
// if it's equal
File.Delete(image2.ToString());
}
Run Code Online (Sandbox Code Playgroud)
所以基本上我现在所拥有的是每个文件都是数字的(没有扩展名).我创建了一个数组,然后将其转换为列表.
我使用我的全局变量image1,image2用于比较它们是否相同.
image1并image2随着搜索的进行而改变.所以它改变了索引.
如果我的compare()方法返回true,它将删除第二个图像.
但是,在该compare()方法上,我似乎在此行上收到此异常错误:
BitmapData bmpData2 = bmp2.LockBits(rect, ImageLockMode.ReadOnly, bmp2.PixelFormat);
Run Code Online (Sandbox Code Playgroud) 这是我正在使用的代码.
String imageURL;
Bitmap bitmap = null;
imageURL = "http://graph.facebook.com/" + fbID + "/picture?type=";
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(imageURL)
.getContent());
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中使用此URL时,它会显示图像,但是当尝试在位图中获取它时,它会给出null.
我检查了很多问题,但没有任何帮助.
我明白了
FATAL EXCEPTION: main
java.lang.NoSuchMethodError: android.widget.ImageView.setBackground
at ua.khuta.mobilereception.ProfileActivity.onCreate(ProfileActivity.java:117)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
at android.app.ActivityThread.access$600(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
在这一行
imageView.setBackground(new BitmapDrawable(getResources(),k));
Run Code Online (Sandbox Code Playgroud)
所有在Android 4.4.2上工作正常,但它不适用于较低版本.我的部分内容是
<uses-sdk android:minSdkVersion="10"
android:targetSdkVersion="17"/>
Run Code Online (Sandbox Code Playgroud) 我有一张PNG图片(角落里有透明像素),我正在尝试提取这些透明像素的颜色代码,我使用了一个xxxx网站,它让我可以选择一个像素并返回其颜色代码;
选择透明像素后,我得到:RGB代码:R:34 G:62 B:74
那不是逻辑
我使用PNG图像使用BitmapDrawable //我认为不是正确的方法
Bitmap bitmap =((BitmapDrawable)image.getDrawable()).getBitmap();
有没有办法检查像素是否透明?
谢谢
在某种程度上,一个不那么容易的错误已经潜入这段代码,而我不知道修复它:
static void Main(string[] args)
{
Bitmap bm = new Bitmap(1, 1);
bm.SetPixel(1, 1, Color.AliceBlue);
bm.Save("C:\\Users\\Lasse\\Pictures\\Midlertidigt\\hej.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);//
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
例外:
System.Drawing.dll中发生未处理的"System.ArgumentOutOfRangeException"类型异常
附加信息:参数必须为正且<宽度.
我试图找到一种Bitmap从包含任意大图像的文件加载a的有效方法,在其上放置水印(徽标),并将其保存回文件.
我知道在Android上处理大型位图时会出现内存问题.我不想加载较小的图像样本,因为在将其保存回文件时我需要保持相同的质量.
任何帮助表示赞赏.
我想创建一个TImage组件,并用背景色填充图像。但是我的代码比我预期的要长。
如果我没记错的话,在旧的Delphi版本中,我可以用FloodFill特定的颜色填充整个图像。所以我想我确实错过了一些事情。
有人可以弄清楚如何用更简单的代码填充背景色吗?
Image := TImage.Create(nil);
Image.Position.X := 100;
Image.Position.Y := 100;
Image.Width := 500;
Image.Height := 500;
Image.Bitmap.Width := Trunc(Image.Width);
Image.Bitmap.Height := Trunc(Image.Height);
with Image.Bitmap.Canvas do
begin
BeginScene;
try
Fill.Color := TAlphaColors.Black;
FillRect(RectF(0, 0, Image.Bitmap.Width, Image.Bitmap.Height), 0, 0, [], 1.0);
finally
EndScene;
end;
end;
Run Code Online (Sandbox Code Playgroud) 我正在努力优化性能关键数组比较功能,您可以在这里阅读codereview,我想知道在.NET/F#/ C#中比较不同的原始或复杂类型之间是否存在性能差异.
我正在将一组元组数组与三个整数进行比较.因此,Tuple<int, int, int>[][]表示位图数据中的每个像素的RGB(但偶然地以BGR顺序)值.
原始位是最快比较的吗?整数比双精度或小数快,反之亦然?在某些地方我可以看到有关此主题的良好数据吗?
我正在创建一个从网址下载图像的应用程序,将它们保存在设备上,之后必须将它们加载到具有固定大小的ImageView中.对于下载和保存文件我没有问题但是当我尝试在ImageView中设置图像时我有一个致命的错误,因为我的ImageView图像很大(我认为......).
这是xml文件:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff9f9f"
android:layout_margin="2dip"
android:padding="2dip">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff9f9f"
android:layout_margin="2dip"
android:padding="2dip">
<TextView
android:id="@+id/notizieTitolo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/immagineNotizie"
android:text="Titolo"
android:textColor="#6b71f1"
android:textSize="20dip"
android:layout_margin="2dip" />
<TextView
android:id="@+id/notizieSottoTitolo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/immagineNotizie"
android:text="SottoTitolo"
android:textSize="15dip"
android:layout_margin="2dip"/>
<ImageView
android:background="@drawable/icona"
android:id="@+id/immagineNotizie"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_margin="2dip" />
</RelativeLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
是一个带有ImageView的简单布局,一个位于ImageView右侧的TextView和一个TextView
重要的是ImageView with和height.设置为100dip(如果我计算2dip为保证金).
这个类的代码,图像保存在Bitmap中.
public class Notizia {
String url;
String titolo;
String sottoTitolo;
String nomeImmaginSalvata;
Bitmap immagine;
public Notizia(String tit, String sottoTit, Bitmap imm, String lk){
titolo = tit;
sottoTitolo = sottoTit;
immagine = …Run Code Online (Sandbox Code Playgroud)